All Your Base (Small)

Each test string is a numeral in an unknown base, with distinct symbols for distinct digits; find the smallest value it can represent, avoiding leading zeros.

Medium6GreedyMathImplementationStringNo attempts yetTime limit5sMemory limit512 MB

Problem

In A.D. 2100, aliens came to Earth. They wrote a message in a language nobody can read, and next to it they wrote a row of symbols. The conclusion is that the symbols spell out a number: the number of seconds before war begins.

Nobody knows what any single symbol means. Each symbol stands for one digit, and two different symbols stand for two different digits, but which symbol is which digit, and what base the aliens use, are both unknown. If they wrote ab2ac999, they could have meant 31536000 in base 10, exactly one year, or 12314555 in base 6, which is 398951 seconds, about four and a half days. Three things are certain: the number is positive, the aliens never start a number with a zero, just as we do not, and they do not use unary (base 1).

Determine the smallest value the message can stand for, that is, the minimum possible number of seconds before war begins.

Input

The first line contains one integer TT. Each of the next TT lines contains one string. The string contains only characters in the 'a' to 'z' and '0' to '9' ranges, with no spaces and no punctuation, and it is the message the aliens left. A character such as '7' in the string is only a symbol, so it does not have to mean the digit 7. The test cases are independent: they can use different bases, and the same symbol can mean a different digit in each of them.

Limits

  • 1T1001 \le T \le 100
  • The answer never exceeds 101810^{18}.
  • 11 \le the length of each line <10< 10

Output

For each test case, print one line in the following format.

Case #X: V

XX is the case number, starting from 1, and VV is the minimum number of seconds before war begins.