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 third;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

public class ThirdProblem {
	private int a;
	private int b;
	private int number;
	private HashMap<Integer,HashMap<Integer, Boolean>> hash;


	public ThirdProblem(int a,int b){
		this.a=a;
		this.b=b;
		hash=new HashMap<Integer, HashMap<Integer,Boolean>>();
		for(int i=a;i<=b;i++){
			calculateCouples(i);
		}
	}


	private void calculateCouples(int n) {		
		String original=n+"";		
		for(int i=original.length()-1;i>0;i--){
			String pre=original.substring(i);
			String post=original.substring(0,i);
			int m=Integer.parseInt(pre+post);
			if(m>n && m<=b){
				HashMap<Integer, Boolean> mm=hash.get(m);
				if(mm==null)
					mm=new HashMap<Integer, Boolean>();
				if(mm.get(n)==null){
					mm.put(n, true);
					hash.put(m, mm);
					number++;
				}
			}
		}

	}
	public int getNumber() {
		return number;
	}

	public static void main(String[] args) throws Exception {
		File input=new File("input3"+File.separatorChar+"input4.in");
		File output=new File("output3"+File.separatorChar+"output4.txt");
		PrintWriter pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(output)));

		BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(input)));
		br.readLine();
		String current=br.readLine();
		int cont=1;
		while(current!=null && current.trim().length()!=0){
			StringTokenizer st=new StringTokenizer(current," ");
			int a=Integer.parseInt(st.nextToken());
			int b=Integer.parseInt(st.nextToken());
			int number=new ThirdProblem(a, b).getNumber();
			pw.append("Case #"+cont+": "+number);
			cont++;
			current=br.readLine();
			if(current!=null)
				pw.append("\n");
			pw.flush();
			System.out.println(cont);
		}
		System.out.println("Finished");
	}


}
