
The first line contains 'T', denoting the number of tests.
For each Test :
The only line contains two space-separated integers 'N' and 'K'.
For each test, print one integer, denoting the number appearing at K-th position when the array 'A' is sorted in lexicographical order.
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= 'T' <= 10^3
1 <= 'K' <= 'N' <= 10^9
Time Limit: 1 sec
Algorithm:
E.g., All numbers [1, 120, 16582, 100000, 18572896] or any other number starting with digit' 1' will always be smaller than number '2' in lexicographical order.
The above observation applies to all the positions of digits.
Rather than checking all numbers starting with the digit 'x', and then moving to digit x+1, we can optimize it by calculating the count of numbers in the range [1...N], starting with digit 'x'.
If the above method is applied for all digits [0, 9], we can find the first digit of our answer. Repeat this method for all positions in the same manner.
Algorithm: