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

package recycled;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.TreeSet;

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    try {
            FileInputStream fstream = new FileInputStream("C-small-attempt0.in");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            FileWriter outFile = new FileWriter("out.txt");
            PrintWriter out = new PrintWriter(outFile);
            String line;
            line = br.readLine();
            int cases = Integer.parseInt(line);
            for (int i=1; i<=cases;i++){
                line = br.readLine();
                String[] values = line.split(" ");
                int A = Integer.parseInt(values[0]);
                int B = Integer.parseInt(values[1]);
                int total=0;
                for (int n=A; n<=B;n++){
                    TreeSet<String> solutions = new TreeSet<String>();
                    for (int k=1;k<values[1].length();k++){
                        String m = circshift(n,k);
                        int mVal = Integer.parseInt(m);
                        if(mVal<=B && mVal!=n  && mVal> n && !m.startsWith("0") && m.length()==Integer.toString(n).length() )
                        {
                            solutions.add(m);
                        }                   
                    }
                    total+=solutions.size();
                }
                out.println("Case #"+i+": "+total);
            }
            in.close();
            out.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

}

    private static String circshift(int n, int k) {
        String nString=Integer.toString(n);
        int len=nString.length();
        String m =  nString.substring(len-k)+nString.subSequence(0, len-k);
        //System.out.println(n+" "+m);
        return m;
    }

}

import java.util.Scanner;


public class Recycled {
	/*
	private static boolean areTwoMirrors(int n, int m, int A, int B) {
		if (A > n || n >= m || m > B) return false;
		
		String mStr = Integer.toString(m);
		String nStr = Integer.toString(n);
		if (mStr.charAt(0) == '0' || nStr.charAt(0)=='0') return false;
		if (mStr.length() != nStr.length()) return false;
		for (int i=1; i < nStr.length(); i++) {
			nStr = nStr.substring(1) + nStr.charAt(0);
			if (nStr.equals(mStr)) return true;
		}
		return false;
	}*/
	static int[] powersOfTen = new int[7];
	private static int process(int A, int B) {
		//boolean[] hasVisited = new boolean[B+1];
		int numDigits = Integer.toString(A).length();
		int count = 0;
		for (int i = A; i < B; i++) {
			int num = i;
			for (int start = 1; start < numDigits; start++) {
				int firstDigit = num/powersOfTen[numDigits-1];
				num -= firstDigit*powersOfTen[numDigits-1];
				num *= 10;
				num += firstDigit;
				if (num <= B && num > i) {
					//if (!areTwoMirrors(i,num,A,B)) System.out.println("Jlfwje");
					//System.out.println(i+" "+num);
					count++;
				}
			}
		}
		return count;
	}
	public static void main(String[] args) {
		int currPower = 1;
		for (int i=0; i < powersOfTen.length; i++) {
			powersOfTen[i] = currPower;
			currPower *= 10;
		}
		Scanner in = new Scanner(System.in);
		int T = in.nextInt();
		for (int i=0; i < T; i++) {
			int A = in.nextInt();
			int B = in.nextInt();
			System.out.println("Case #"+(i+1)+": "+process(A,B));
		}
		
		//System.out.println(process(A,B));
	}
}
