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();
    }
}

package codejam2012.qr;

import codejam.common.CodeHelper;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Chen Ling
 */
public class ProblemC {

    private static String SMALL_IN_FILE_NAME = "/codejam2012/qr/C-small.in";
    private static String LARGE_IN_FILE_NAME = "/codejam2012/qr/C-large.in";
    private static String SMALL_OUT_FILE_NAME = "/codejam2012/qr/C-small.out";
    private static String LARGE_OUT_FILE_NAME = "/codejam2012/qr/C-large.out";

    public static int solve(int start, int end) {
        int total = 0;
        for (int number = start; number < end + 1; number++) {
            String pairN = String.valueOf(number);
            if (pairN.length() == 1) {
                continue;
            }
            for (int i = 1; i < pairN.length(); i++) {
                String pairM = pairN.substring(i, pairN.length()) + pairN.substring(0, i);
                int pairMI = Integer.parseInt(pairM);
                if (pairMI >= start && pairMI > number && pairMI <= end) {
                    total++;
//                    System.out.print("Pair(" + pairN + ", " + pairM + ")");
//                    System.out.print(", Ture");
//                    System.out.println("");
                }

            }
        }
//        System.out.println("Total: " + total);
        return total;
    }

    public static void main(String[] args) {
//        ProblemC.solve(1111, 2222);
//        List<String> smallLinesIn = CodeHelper.loadInputLines(SMALL_IN_FILE_NAME);
//        List<String> smallOuts = new ArrayList<String>();
//        for (int i = 1; i < smallLinesIn.size(); i++) {
//            String[] numbers = smallLinesIn.get(i).split(" ");
//            int start = Integer.parseInt(numbers[0]);
//            int end = Integer.parseInt(numbers[1]);
//            smallOuts.add("Case #" + i + ": " + ProblemC.solve(start, end));
//        }
//        CodeHelper.writeOutputs(SMALL_OUT_FILE_NAME, smallOuts);

        List<String> largeLinesIn = CodeHelper.loadInputLines(LARGE_IN_FILE_NAME);
        List<String> largeOuts = new ArrayList<String>();
        for (int i = 1; i < largeLinesIn.size(); i++) {
            String[] numbers = largeLinesIn.get(i).split(" ");
            int start = Integer.parseInt(numbers[0]);
            int end = Integer.parseInt(numbers[1]);
            largeOuts.add("Case #" + i + ": " + ProblemC.solve(start, end));
        }
        CodeHelper.writeOutputs(LARGE_OUT_FILE_NAME, largeOuts);
    }
}
