Problem E: Shift to Double (“Wildcard”)


Description

Does there exist a number (a positive integer) such that, if you move its right-most digit (the least significant) to the front (to the most significant), the result is twice the original? This was asked one day at lunch in informal chat where Freeman Dyson and other mathematicians had met together. Dyson answered nigh on immediately something like, “Of course. However, the smallest such integer is 18 digits long.”

The lunch group was considering the numbers in base 10. Of course, which base one considers matters. One can generalize to consider any base, and consider any multiplier, not just 2.

Let us define dyson(b, x) to be the smallest positive integer that, written in base b, when we move its right-most (least significant) digit to the front (most significant), the resulting number is x times the original, if such a number exists. For example, dyson(4, 2) = 1024, since 1024 * 24 = 2104 (and it is the smallest such number).

Note that a list of digits commencing with 0 (e.g., 01234) is not considered as a valid solution. So the number for dyson(b, x) is the smallest positive integer in base b written normally with no preceding zeroes.


Input

Your program should find the requested Dyson numbers. The first line will consist of a single number t designating the number of Dyson numbers to compute. Then t lines follow, each with a pair of integers designating the base b (2 < b ≤ 10) and multiplier x (2 ≤ x < b).


Sample Input

3
4 2
10 4
10 6

Output

For each b and x pair from the input, compute and print on a single line dyson(b, x). If no solution exists for a particular dyson(b, x), print “undefined” on the line for it.


Sample Output

102
102564
1016949152542372881355932203389830508474576271186440677966