import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class Recycle {

	private static HashMap<Integer, HashSet<Integer>> map = new HashMap<Integer, HashSet<Integer>>();
	private static HashSet<Integer> toSkip = new HashSet<Integer>();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int T = reader.nextInt();
		for (int tc = 1; tc <= T; tc++) {
			int a = reader.nextInt();
			int b = reader.nextInt();
			long before = System.currentTimeMillis();
			int res = doit(a, b);
			System.out.printf("Case #%d: %d\n", tc, res);
			long after = System.currentTimeMillis();
//			System.out.println("time taken: " + (after - before));
		}
	}

	private static int doit(int a, int b) {
		int count = 0;
		HashSet<Integer> skip = new HashSet<Integer>(toSkip);
		for (int i = a; i <= b; i++) {
			if (skip.contains(i))
				continue;
			HashSet<Integer> arr = generate(i);
//			if(arr.size() > 1) {
//				map.put(i, arr);
//			}
			// System.out.println(i + " --> " +
			// Arrays.deepToString(arr.toArray()));
			int temp = 0;
			if (arr.size() > 1) {
				for (int x : arr) {
					if (x >= a && x <= b) {
						temp++;
						skip.add(x);
					}
				}
				count += temp * (temp - 1) / 2;
			}
//			System.out.println("not skipping " + i + " gen: " + arr.size()+ " " + (temp * (temp - 1) / 2));
		}
		return count;
	}

	private static HashSet<Integer> generate(int num) {
		if(map.containsKey(num)) {
			HashSet<Integer> list = map.get(num);
			return list;
		}
		HashSet<Integer> list = new HashSet<Integer>();
		String s = "" + num;
		list.add(num);
		int len = s.length();
		for (int i = 1; i < len; i++) {
			String temp = s.substring(i) + s.substring(0, i);
			// System.out.println("temp: " + temp);
			if (temp.charAt(0) == '0')
				continue;
			int x = Integer.parseInt(temp);
			if( x <= 2000000) {
				list.add(x);
			}
		}
		if(list.size() > 1) {
			map.put(num, list);
		} else {
			toSkip.add(num);
		}
		return list;
	}

}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Recycled {
    private String strLine;
    private BufferedReader f;
    private PrintWriter out;
    private ArrayList<Integer> hasil;
    
    public Recycled() {
        strLine = new String();
	try {
		f = new BufferedReader(new FileReader("c0.in"));
		out = new PrintWriter(new BufferedWriter(new FileWriter("recycled.out")));
	} catch(IOException i) {
	}
	hasil = new ArrayList<Integer>();
    }
    
    public void find(int A, int B) {
        byte[] n;
        byte[] m;
        boolean isFind = false;
        int count = 0;
        for (int i = A; i < B; i++) {
            for (int j = i+1; j <= B; j++) {
                n = Integer.toString(i).getBytes();
                m = Integer.toString(j).getBytes();
                if (n.length == m.length) {
                    for (int k = 1; k < m.length && !isFind; k++) {
                        m = cls(m,8);
                        if (Arrays.equals(n, m)) {
                            isFind = true;
                        }
                    }
                    if (isFind) {
                        count++;
                        isFind = false;
                    }
                }
                
            }
        }
        hasil.add(count);
    }
    
    int byteArrayToInt(byte [] b) {
        return (b[0] << 24)
                + ((b[1] & 0xFF) << 16)
                + ((b[2] & 0xFF) << 8)
                + (b[3] & 0xFF);
    }

    byte[] intToByteArray(int value) {
        return new byte[] {
                (byte)(value >>> 24),
                (byte)(value >>> 16),
                (byte)(value >>> 8),
                (byte)value};
    }
    
    public byte[] cls(byte[] b, int k){
    
        byte[] hasil1 = b;
        byte temp = hasil1[0];
        System.arraycopy(b, 1, hasil1, 0, b.length-1);
        hasil1[b.length-1] = temp;
        return hasil1;
    }
    
    public static void main(String[] args) throws IOException {
        Recycled rec = new Recycled();
        int T;
	int A;
        int B;
	StringTokenizer st;
	
	T = Integer.parseInt(rec.f.readLine());
        System.out.println("T = " + T);
	if (T > 50 || T < 1) {
            System.out.println("Error");
	} else {
            for (int i = 0; i < T; i++) {
                if ((rec.strLine = rec.f.readLine()) != null) {
                    st = new StringTokenizer(rec.strLine);
                    A = Integer.parseInt(st.nextToken());
                    B = Integer.parseInt(st.nextToken());
                    rec.find(A,B);
                }
            }
                for (int x = 0; x < rec.hasil.size(); x++) {
                        rec.out.println("Case #" + (x + 1)+ ": " +rec.hasil.get(x));
                }
		rec.out.close();                                  // close the output file
		System.exit(0);
	}
    }
}
