package qual;

import java.util.Scanner;

public class RecycledNumbers {

	public static void main(String[] args) {
		new RecycledNumbers().run();

	}

	private void run() {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int t = 0; t < T; t++) {
			int A = sc.nextInt();
			int B = sc.nextInt();
			int result = solve(A, B);
			System.out.printf("Case #%d: %d\n", t + 1, result);
		}
	}

	public int solve(int A, int B) {
		int result = 0;
		for (int i = A; i <= B; i++) {
			int dig = (int) Math.log10(A/*same as B*/) + 1;
			int aa = i;
			for (int d = 0; d < dig - 1; d++) {
				aa = (aa % 10) * (int)Math.pow(10, dig - 1) + (aa / 10);
				if (i == aa) {
					break;
				}
				if (i < aa && aa <= B) {
//					System.out.printf("(%d, %d) ", i, aa);
					result++;
				}
			}
		}
		return result;
	}

}

package qualification;

import java.io.FileInputStream;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: Yuri
 * Date: 14.04.12
 * Time: 12:18
 * To change this template use File | Settings | File Templates.
 */
public class Task3 {

    static int divider;

    static int recycle(int a) {
        int mod = a % divider;
        int digit = a / divider;
        return (mod * 10) + digit;
    }

    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(new FileInputStream("task31.txt"));
        int N = scanner.nextInt();
        Set<Integer> set = new HashSet<Integer>();
        for (int n = 0; n < N; n++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();

            int digitsCount = String.valueOf(a).length();
            divider = (int) Math.pow(10, digitsCount - 1);
            int count = 0;
            for (int c = a; c <= b; c++) {
                set.clear();
                int recycled = c;
                for (int i = 0; i < digitsCount - 1; i++) {
                    recycled = recycle(recycled);
                    if (recycled <= b && recycled > c) {
                        if (! set.contains(recycled)) {
                            set.add(recycled);
                            count++;
                        }
                    }
                }
            }
            System.out.println("Case #" + (n + 1) + ": " + count);
        }
    }
}
