// Arup Guha
// 4/8/2016
// Solution to 2016 Code Jam Qualification Problem A: Counting Sheep (written in contest).

import java.util.*;

public class a {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();;

		// Process each case, only 0 is impossible.
		for (int loop=1; loop<=numCases; loop++) {
			long n = stdin.nextLong();
			long res = solve(n);
			if (res == -1) System.out.println("Case #"+loop+": INSOMNIA");
			else System.out.println("Case #"+loop+": "+res);
		}
	}

	// Solves problem for n.
	public static long solve(long n) {

		// bit i of mask stores if we've seen digit i.
		int mask = 0;

		// mult stores what we're multiplying by.
		long mult = 1;

		// Keep on going until we have seen all the digits.
		while (mask != 1023) {

			// Next term to evaluate.
			long term = n*mult;

			// Peel off each digit and mark it.
			while (term > 0) {
				int digit = (int)(term%10);
				mask = mask | (1<<digit);
				term /= 10;
			}

			// Got the answer or we've been looking too long.
			if (mask == 1023) return n*mult;
			if (mult == 10000000) break;
			mult++;
		}

		// If we get there, it doesn't work.
		return -1;
	}
}