/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package year_2012.qualification;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author paul
 * @date 14 Apr 2012
 */
public class QuestionC {

    private static Set<Integer> getCycle(int x) {

        String s = Integer.toString(x);
        Set<Integer> set = new HashSet<Integer>();
        set.add(x);
        for (int c = 1; c < s.length(); c++) {
            String t = s.substring(c).concat(s.substring(0, c));
            set.add(Integer.parseInt(t));
        }
        return set;
        
    }

    private static Set<Integer> mask(Set<Integer> set, int a, int b) {
        Set<Integer> result = new HashSet<Integer>();

        for (int x : set) {
            if (x >= a && x <= b) {
                result.add(x);
            }
        }
        
        return result;

    }


    public static void main(String[] args) throws FileNotFoundException, IOException {

        String question = "C";

//        String name = "large";
        String name = "small-attempt0";
//        String name = "test";

        String filename = String.format("%s-%s", question, name);

        BufferedReader input = new BufferedReader(
                new FileReader(String.format("/home/paul/Documents/code-jam/2012/qualification/%s.in",
                filename)));

        String firstLine = input.readLine();

        PrintWriter pw = new PrintWriter(new File(
                String.format("/home/paul/Documents/code-jam/2012/qualification/%s.out", filename)));


        int T = Integer.parseInt(firstLine);

        // Loop through test cases.
        for (int i = 0; i < T; i++) {

//             Read data.
            String[] tokens = input.readLine().split(" ");

//            int N = Integer.parseInt(input.readLine());

            int A = Integer.parseInt(tokens[0]);
            int B = Integer.parseInt(tokens[1]);

//            System.out.format("%d, %d\n", A, B);

            boolean[] used = new boolean[B+1];
            int total = 0;
            for (int n = A; n <= B; n++) {
                if (!used[n]) {
                    Set<Integer> set = mask(getCycle(n), A, B);
                    int k = set.size();
                    total += (k * (k-1))/2;
                    for (int m : set) {
                        used[m] = true;
                    }
                }
            }

            pw.format("Case #%d: %d\n", i + 1, total);
        }

        pw.close();

    }

}

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


public class RecycledNumbers {

	private static int noOfDigits(int n){
		if (n < 10) return 1;
		return 1 + noOfDigits(n/10);
	}

	private static int raisedTo(int n, int m){
		if (n==0) return 0;
		else if (m==0) return 1;
		else if (m==1) return n;
		return n*raisedTo(n, m-1);
	}

	private static int rotateBy(int n, int s, int nod){
		if (s == 0) return n;
		int N = noOfDigits(n);
		int pow = raisedTo(10, nod-1);
		int MSB = (N==nod)? n/pow: 0;
		return rotateBy((n%pow)*10+MSB, s-1, nod);
	}

	private static int[] giveDistinctNos(int n, int min, int max){
		int s = 0, r = n, j=0;
		int N = noOfDigits(r);
		int A[] = new int[N];
		for (int i=0; i<N; i++){
			for (j=0; j<s; j++){
				if (A[j] == r)
					break;
			}
			if (j == s){
				A[s++] = r;
			}
			r = rotateBy(r, 1, N);
			while (r<min || r>max || N != noOfDigits(r))
				r = rotateBy(r, 1, N);
		}
		return A;
	}

	private static int nC2(int n){
		return n*(n-1)/2;
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		//		for (int i=0; i< 2000000; i++);

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int T = new Integer(in.readLine()).intValue();
		int A = 0, B = 0, ans = 0;
		String str[];
		int X[];
		for (int i=0; i<T; i++){
			ans = 0;
			str = in.readLine().split(" ");
			A = new Integer(str[0]).intValue();
			B = new Integer(str[1]).intValue();
			X = new int[B-A+1];
			for (int j=A; j<B+1; j++){
				if (X[j-A] != 1){
					int N[] = giveDistinctNos(j, A, B);
					int n=0;
					for (int k=0; k<N.length; k++){
						if (N[k] == 0)
							break;
						X[N[k]-A] = 1;
						n++;
					}
					ans += nC2(n);
				}
			}
			System.out.print("Case #"+(i+1)+": "+ans);
			System.out.println();
		}
	}

}
