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.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class C {
    static String str = "C:\\carl\\cl.txt";
    static String text = "";

    static int A, B, T, cnt;
    static HashMap hm;


    public static void main(String[] args) {

        try {
            int count = 0;

            hm = new HashMap();

            File file = new File(str);
            BufferedReader br = new BufferedReader(new FileReader(file));
            PrintWriter outer = new PrintWriter(new FileWriter("C:\\carl\\clo.txt"));


            while((text = br.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(text);

                if(count == 0) {
                    T = Integer.parseInt(st.nextToken());
                    count++;
                } else  {

                    A = Integer.parseInt(st.nextToken());
                    int n = A;
                    B = Integer.parseInt(st.nextToken());

                    while (n != B) {
                        char[] temp = Integer.toString(n).toCharArray();
                        scramble(temp, n);
                        n++;
                    }
                    outer.println("Case #" + count + ": " + hm.entrySet().size());
                //System.out.println(hm.entrySet().size());
                hm.clear();
                count++;
                    
                }
                
            }

            
            outer.close();

        } catch(IOException ex) {
            System.out.println("Error with IO you foooool");
        } catch(Exception ex) {
            System.out.println("Generic Error. Noob.");
            ex.printStackTrace();
        }
    }

    public static void scramble(char[] temp, int n) {
        Integer[] no = new Integer[temp.length];
        for(int i=0; i<temp.length;i++) { no[i] = Integer.parseInt( Character.toString(temp[i]) ); }
        //Integer[] test = {1, 2, 3, 4, 5};
        for (int i = 0; i < temp.length - 1; i++) {
            Collections.rotate(Arrays.asList(no), -1);

            int m = trans(no);
            //System.out.println(n + " " + m);
            if( n < m && m <= B && no[0] != 0 ) {

                if( !hm.containsKey(n + " " + m)) {
                    hm.put(n + " " + m, n + " " + m);
                    //System.out.println(n + ", " + m);
                }

                //System.out.println(b);
            }
        }


    }

    public static int trans(Integer[] a) {
        String s = "";
        for(int i : a) {
            s = s + i;
        }
        //System.out.println(" @@ " + s);
        return Integer.parseInt(s);
    }

}
