Counter Culture (Large)
Time limit5sMemory limit512 MB
Starting from 1, reach N with steps of adding 1 or reversing decimal digits, using the fewest spoken numbers.
Problem
At the counting poetry slam, a performer takes the microphone, picks a number , and counts aloud from 1 to . She says 1 first, then repeatedly says the number that is 1 greater than the number she just said, and she stops after saying .
It is your turn to perform. Plain counting is tedious, so you add one more move: instead of adding 1 to the last number, you may reverse its digits and say that number, dropping any leading zeros the reversal creates. After saying "16" you may say either "17" or "61", and after saying "2300" you may say either "2301" or "32". You may reverse as many times as you want during a performance, or not at all.
The first number you say must be 1. Find the fewest numbers you need to say in order to reach . Both 1 and count toward this 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 . Each of the next lines contains one integer , the number you must reach.
Limits
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from 1 and is the minimum count of numbers you need to say.
Note
In the second test case of the sample, reversing never helps, so counting straight up to 19 is optimal.
In the third test case, the best plan counts up to 12, reverses to 21, and then counts up to 23. The numbers said are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 22, 23.