Multi-base happiness (small)

Find the smallest integer above 1 that reaches 1 under the digit-square-sum process in every base of each test case.

Medium4SimulationHash mapMathNo attempts yetTime limit5sMemory limit512 MB

Problem

Replace an integer NN by the sum of the squares of its digits. A happy number is a number that reaches 1 when you repeat this process. Starting from 82:

8*8 + 2*2       = 64 + 4    = 68,  repeat:
6*6 + 8*8       = 36 + 64   = 100, repeat:
1*1 + 0*0 + 0*0 = 1 + 0 + 0 = 1 (happy!)

The process reached 1, so 82 is a happy number.

Being happy in base bb means writing the number in base bb, adding the squares of those digits, and repeating until 1 appears. The same number can be happy in one base and not happy in another. The decimal number 82 is written 10001 in base 3, and in base 3 it is not happy.

Some of the bases got together (yes, they are organized) and hired you for a job: find the smallest integer greater than 1 that is happy in all of the given bases.

Input

The first line contains the number of test cases TT. Each of the next TT lines holds one test case: a space separated list of distinct bases in increasing order.

Limits

  • 1T421 \le T \le 42
  • each line lists 2 or 3 bases
  • every base bb in the input satisfies 2b102 \le b \le 10

Output

For each test case, print one line:

Case #X: K

where XX is the test case number starting from 1, and KK is the decimal form of the smallest integer greater than 1 that is happy in all of the given bases.