Tidy Numbers (Large)

Given N up to 10^18, find the largest number not exceeding N whose decimal digits are in non-decreasing order.

Medium5GreedyMathImplementationStringNo attempts yetTime limit5sMemory limit512 MB

Problem

Tatiana likes to keep things tidy. Her toys are sorted from smallest to largest, her pencils from shortest to longest, and her computers from oldest to newest. One day, while practicing her counting, she noticed that some integers have their digits in non-decreasing order when written in base 10 with no leading zeroes. Examples are 8, 123, 555, and 224488. She decided to call such an integer a tidy number. Numbers without this property, such as 20, 321, 495, and 999990, are not tidy.

She has just counted every positive integer from 1 to NN in ascending order. What was the last tidy number she counted?

Input

The first line contains the number of test cases TT. Each of the next TT lines describes one test case with a single integer NN, the last number Tatiana counted.

Limits

  • 1T1001 \le T \le 100
  • 1N10181 \le N \le 10^{18}

Output

For each test case, print one line of the form Case #x: y, where xx is the test case number starting from 1 and yy is the last tidy number Tatiana counted.