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 codejam;

import java.io.*;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class RecycledNumbers {


    public static final String LINE_SEPARATOR = System.getProperty("line.separator");

    public static int recycleNum(int a, int b) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        int n = String.valueOf(a).length();
        int c = 0;
        for (int i = a; i <= b; i++) {
            for (int j = 1; j < n ; j++) {
                int rNum = rotateRight(i, j);
                if (i < rNum && rNum <= b) {
                    if (!map.containsKey(rNum) || map.get(rNum) != i) {
                        map.put(rNum, i);
                        c++;
                    }
                }
            }
        }
        return c;
    }

    private static int rotateRight(int i, int j) {
        int length = String.valueOf(i).length();
        int sP = (int) Math.pow(10, j);
        int bP = (int) Math.pow(10, length - j);
        int rem = i % sP;
        return ((i - rem) / sP) + (rem * bP);
    }

    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(new File("D:\\codejam\\C-small-attempt0.in"));
        Writer writer = new OutputStreamWriter(new FileOutputStream("D:\\codejam\\C-small-attempt.out"));
        Integer n = Integer.valueOf(scanner.nextLine());
        for (int i = 1; i <= n; i++) {
            System.out.println();
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            writer.write("Case #" + i + ": " + recycleNum(a, b));
            writer.write(LINE_SEPARATOR);
        }
        scanner.close();
        writer.close();
    }

}
