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

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;

public class Third {

    public static void main(String[] args) throws FileNotFoundException {
        String path = ClassLoader.getSystemClassLoader().getResource("codejam/").getPath();
        Third worker = new Third();

        worker.scan(new File(path, "third-s.in"), new File(path, "third-s.out"));
//        worker.scan(new File(path, "third-x.in"), new File(path, "third-x.out"));
    }

    private void scan(File input, File output) throws FileNotFoundException {
        Scanner in = new Scanner(input);
        PrintWriter out = new PrintWriter(output);

        int n = Integer.parseInt(in.nextLine());
        for (int i = 0; i < n; i++) {
            out.printf("Case #%d: %s\n", i + 1, solve(in.nextLine()));
        }

        out.flush();
        out.close();
        in.close();
    }

    public String solve(String line) {
        String[] splited = line.split(" ");

        return "" + solve(Integer.parseInt(splited[0]), Integer.parseInt(splited[1]));
    }

    public int solve(int a, int b) {
        if (a < 10 || a == b) {
            return 0;
        }

        int shiftCount = String.valueOf(a).length() - 1;
        int counter = 0;
        Map<String, Boolean> duplicate = new HashMap<String, Boolean>(shiftCount);

        for (int n = a; n <= b; n++) {
            duplicate.clear();
            for (int i = 1; i <= shiftCount; i++) {
                int m = shift(n, i);
                if (m < a || m > b || m == n || m > n) {
                    continue;
                }
                String key = "" + n + ":" + m;
                if (!duplicate.containsKey(key)) {
                    counter++;
                    duplicate.put(key, Boolean.TRUE);
                }
                //System.out.println("(" + n + ", " + m + ") ");
                //System.out.print("\'" + n + ":" + m + "\', ");

            }
        }
        return counter;
    }

    private int shift(int a, int n) {
        String s = String.valueOf(a);
        String head = s.substring(0, s.length() - n);
        String tail = s.substring(s.length() - n);
        return Integer.parseInt(tail + head);
    }
}
