Counter Culture (Small)

Count from 1 to N where each step adds 1 or reverses the digits, and find the fewest spoken numbers.

Medium5BFSGraphNo attempts yetTime limit5sMemory limit512 MB

Problem

At the counting poetry slam, a performer takes the microphone, picks a number NN, and counts aloud from 1 to NN. She says 1 first, then keeps saying the number that is 1 greater than the number she just said, and stops once she has said NN.

It is your turn now. You find the routine tedious, so you add a twist that speeds it up. Instead of adding 1 to the previous number, you may reverse its digits, dropping any leading zeros the reversal creates. After saying 16 you may say either 17 or 61. After saying 2300 you may say either 2301 or 32. Within one performance you may reverse as many times as you want, or not at all.

The first number you say must be 1. What is the fewest numbers you need to say in order to reach NN? Both 1 and NN count toward that total. If you say the same number several times, each of those times counts separately.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains one integer NN, the number you must reach.

Limits

  • 1T1001 \le T \le 100
  • 1N1061 \le N \le 10^6

Output

For each test case, print one line in the form "Case #x: y", where x is the test case number starting from 1 and y is the fewest numbers you need to say.

Notes

For N=19N = 19, reversing never helps, so counting straight up to 19 is optimal.

For N=23N = 23, the best plan is to count up to 12, reverse to 21, then keep counting to 23. The numbers said are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 22, 23.