package qual;

import java.util.Scanner;

public class RecycledNumbers {

	public static void main(String[] args) {
		new RecycledNumbers().run();

	}

	private void run() {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int t = 0; t < T; t++) {
			int A = sc.nextInt();
			int B = sc.nextInt();
			int result = solve(A, B);
			System.out.printf("Case #%d: %d\n", t + 1, result);
		}
	}

	public int solve(int A, int B) {
		int result = 0;
		for (int i = A; i <= B; i++) {
			int dig = (int) Math.log10(A/*same as B*/) + 1;
			int aa = i;
			for (int d = 0; d < dig - 1; d++) {
				aa = (aa % 10) * (int)Math.pow(10, dig - 1) + (aa / 10);
				if (i == aa) {
					break;
				}
				if (i < aa && aa <= B) {
//					System.out.printf("(%d, %d) ", i, aa);
					result++;
				}
			}
		}
		return result;
	}

}

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

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mayorandrew
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        BufferedReader input = null;
        try {
            input = new BufferedReader(new FileReader("input.txt"));
            FileOutputStream output = new FileOutputStream("output.txt");
            
            int N = Integer.parseInt(input.readLine());
            for(int i=0; i<N; i++){
                String[] line = input.readLine().split("\\ ");
                int a = Integer.parseInt(line[0]);
                int b = Integer.parseInt(line[1]);
                
                System.out.println("###" + i+":"+a+" - "+b);
                boolean[] check = new boolean[b-a+1];
                for(int j=0; j<b-a+1; j++){
                    check[j]=true;
                }
                int n = 0;
                for(int k=a; k<=b; k++){
                    String r = Integer.toString(k);
                    int l = r.length();
                    if(check[k-a]){
                        int nn = 0;
                        int[] numbers = new int[l];
                        for(int s=1; s<l; s++){
                            int num = Integer.parseInt(r.substring(l-s, l)+r.substring(0, l-s));
                            if(num>=a && num<=b && num!=k){
                                boolean found = false;
                                for(int j=1; j<l; j++){
                                    if(numbers[j]==num){
                                        found=true;
                                    }
                                }
                                if(!found){
                                    nn++;
                                    numbers[s]=num;
                                    check[num-a]=false;
                                    //System.out.println(k+" - "+num+" v");
                                }
                            }else{
                                numbers[s]=0;
                                //System.out.println(k+" - "+num);
                            }
                        }
                        n+=nn*(nn+1)/2;
                        //System.out.println("#" + "add " + nn);
                    }
                }
                output.write(("Case #"+(i+1)+": "+n+"\n").getBytes());
            }
            
            input.close();
            output.close();
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
