#P1870F. Lazy Numbers
Lazy Numbers
No submission language available for this problem.
Description
You are given positive integers and . For each number from to , we write its representation in the number system with base (without leading zeros) and then sort the resulting array in lexicographic order as strings. In the sorted array, we number the elements from to (i.e., indexing starts from ). Find the number of values such that the representation of number is at the -th position in the sorted array of representations.
Examples of representations: in any number system is equal to , with is written as , and with is written as .
The first line contains a single integer () — the number of test cases. This is followed by a description of the test cases.
The only line of each test case contains integers and (, ).
For each test case, output a single integer — the number of values such that the representation of number in the number system with base is at the -th position after sorting.
Input
The first line contains a single integer () — the number of test cases. This is followed by a description of the test cases.
The only line of each test case contains integers and (, ).
Output
For each test case, output a single integer — the number of values such that the representation of number in the number system with base is at the -th position after sorting.
Note
In the first test case, for numbers and , their representations are at positions and respectively.
In the second test case, the sorted array is , and only the representations of numbers and are at the required positions.
In the third test case, the sorted array is , and only the number is at its position.