// Arup Guha
// 4/8/2017
// Solution to 2017 Code Jam Qualification Problem B: Tidy Numbers

import java.util.*;

public class b {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();

		// Process each case - store as character array.
		for (int loop=1; loop<=numCases; loop++) {
			char[] num = stdin.next().toCharArray();
			System.out.println("Case #"+loop+": "+solve(num));
		}

	}

	public static long solve(char[] num) {

		int down = -1;
		
		// Find the first "down" pair.
		for (int i=0; i<num.length-1; i++) {
			if (num[i+1] < num[i]) {
				down = i;
				break;
			}
		}

		// The number itself is tidy; return it.
		if (down == -1) return Long.parseLong(new String(num));

		// Now, go through all the equal digits, scanning back.
		while (down > 0 && num[down-1] == num[down]) down--;
		
		// Subtract 1 from this slot.
		num[down]--;
		
		// Just change these to 9s...
		for (int i=down+1; i<num.length; i++) num[i] = '9';
		
		// Strip leading 0s and return.
		String s = new String(num);
		while (s.charAt(0) == '0') s = s.substring(1);
		return Long.parseLong(s);
	}
}