import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;

public class Recycled {
    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int t = Integer.parseInt(br.readLine());
            for (int i = 0; i < t; i++) {
                String[] str = br.readLine().split(" ");
                int a = Integer.parseInt(str[0]);
                int b = Integer.parseInt(str[1]);
                System.out.println("Case #" + (i + 1) + ": " + find(a, b));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static int find(int a, int b) {
        HashSet<Long> used = new HashSet<Long>();
        int digits = String.valueOf(a).length();
        if (digits == 1) return 0;
        for (int i = a; i <= b; i++) {
            int s = 10;
            int m = (int) Math.pow(10, digits-1);
            for (int j = 0; j < digits-1; j++) {
                int r = i % s;
                if (r < s/10) continue;
                int q = i / s;
                int rn = r * m + q;
                s *= 10; m /= 10;
                if (i != rn && rn >= a && rn <= b) {
                    long pp;
                    if (rn < i) pp = (long)rn << 30 | i;
                    else pp = (long)i << 30 | rn;
                    if (!used.contains(pp)) {
                        used.add(pp);
                    }
                }
            }
        }
        return used.size();
    }
}

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class codejam_C_small {
	public static void main(String[] args) throws IOException {
		FileUtil.Timer.start();

		Scanner scanner = new Scanner(System.in);
		int numOfTest = scanner.nextInt();
		scanner.nextLine();
		String[] testCases = new String[numOfTest];
		for (int i = 0; i < numOfTest; i++) {
			testCases[i] = scanner.nextLine();
		}

		String[] finalResult = new String[numOfTest];
		for (int i = 0; i < numOfTest; i++) {
			String testCase = testCases[i];
			String result = runTestCase(testCase);
			finalResult[i] = FileUtil.formatTestResult(i, result);
			System.out.println(finalResult[i]);
		}
		FileUtil.Timer.stop();
	}

	private static String runTestCase(String testCase) {
		String[] splits = testCase.split(" ");
		long lowerBound = FileUtil.toNumber(splits[0]);
		long upperBound = FileUtil.toNumber(splits[1]);
		if (lowerBound > upperBound || upperBound < 10) {
			return "0";
		}

		int count = 0;
		// System.out.println("Lower: " + lowerBound + ", Upper: " +upperBound
		// );
		Map<Long, Long> cachedMap = new HashMap<Long, Long>();
		for (long start = lowerBound; start < upperBound; start++) {
			long intermidate = start;
			int numOfShift = String.valueOf(start).length() - 1;
			if (numOfShift < 0) {
				continue;
			}

			for (int i = 0; i < numOfShift; i++) {
				intermidate = shiftLongNumber(intermidate, numOfShift);
				if (intermidate > start && intermidate <= upperBound) {
					if ((cachedMap.containsKey(intermidate) && cachedMap.get(
							intermidate).equals(start))
							|| (cachedMap.containsKey(start) && cachedMap.get(
									intermidate).equals(intermidate))) {
						// System.out.println(start + ", " + intermidate);
					} else {
						cachedMap.put(intermidate, start);
						count++;
					}
				}
			}
		}
		return String.valueOf(count);
	}

	private static long shiftLongNumber(long input, int numOfShift) {
		long lastDigit = input % 10;
		long reminder = input / 10;
		return (long) (Math.pow(10, numOfShift) * lastDigit + reminder);
	}
}
