// Arup Guha
// 5/20/2021
// Solution to Code Jam Round 1C Problem B: Roaring Years

import java.util.*;
import java.math.*;

public class Solution {
	
	public static BigInteger val;
	public static int numD;
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
	
		// Process cases.
		for (int loop=1; loop<=nC; loop++) {
		
			// Store in all forms.
			String num = stdin.next();
			numD = num.length();
			val = new BigInteger(num);
			
			// Must be this or smaller...
			BigInteger res = new BigInteger("1234567891011121314");
			
			// Try concatenating each number of possible values.
			for (int i=2; i<=14; i++) {
				BigInteger tmp = bestForN(i);
				if (tmp.compareTo(res) < 0) res = tmp;
			}
			
			// Ta da!
			System.out.println("Case #"+loop+": "+res);
		}
	}
	
	// n is # of numbers.
	public static BigInteger bestForN(int n) {

		// Binary search the starting number.
		int low = 1, high = 1000000000;
		while (low < high) {
			
			// Form this number.
			int mid = (low+high)/2;
			String tmp = "";
			for (int i=mid; i<mid+n; i++)
				tmp = tmp + i;
			
			// Get this case out of the way w/o forming the number.
			if (tmp.length() > numD) {
				high = mid;
				continue;
			}
			
			// See if it's big enough or not.
			BigInteger mine = new BigInteger(tmp);
			if (mine.compareTo(val) > 0)
				high = mid;
			else
				low = mid+1;
		}
		
		// This is guaranteed to be valid and greater than val.
		String tmp = "";
		for (int i=low; i<low+n; i++)
			tmp = tmp + i;
		return new BigInteger(tmp);
	}	
}