This page is still under construction.

Parts of this page are still being built. What you see may change.

Multi-base happiness (small)

Time limit5sMemory limit512 MB

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

Medium4 of 10

Topics
Simulation, Hash map, Math
Solved
No attempts yet

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

  • 1≤T≤421 \le T \le 42
  • each line lists 2 or 3 bases
  • every base bb in the input satisfies 2≤b≤102 \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.

Examples2

  1. Example 1

    Input
    3
    2 3
    2 3 7
    9 10
    
    Expected output
    Case #1: 3
    Case #2: 143
    Case #3: 91
    
  2. Example 2

    Input
    6
    2 10
    2 3
    9 10
    2 3 4
    8 9 10
    2 9 10
    
    Expected output
    Case #1: 7
    Case #2: 3
    Case #3: 91
    Case #4: 3
    Case #5: 1177
    Case #6: 91