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 year2012.qualification.c;

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class RecycledNumbers {
	public static void main(String[] args) throws Exception{
		//GCJå±é
		String filename = "src/year2012/qualification/c/C-large";
		PrintWriter out = new PrintWriter(new File(filename + ".out"));
		Scanner scan = new Scanner(new File(filename + ".in"));
		
		final int T = scan.nextInt();
		for(int i=0;i<T;i++){
			int ans=0;
			int A = scan.nextInt();
			int B = scan.nextInt();
			
			for(int n=A;n<B;n++){
				int m_memo = -1;
				
				char[] na = String.valueOf(n).toCharArray();
				//ä¾ãã°nã123ã®ã¨ãã231,312ã[A,B)ã®ä¸­ã«ãããèª¿ã¹ã
				//ãã®ä¾ã ã¨k=2åã«ã¼ã
				for(int k=1;k<na.length;k++){
					char[] ma = rotate(na, k);
					int m = Integer.parseInt(String.valueOf(ma));
					if(A <=n
						&& n < m
						&& m <= B
						&& m != m_memo){
						
						m_memo = m;
						ans++;
					}
				}
				
			}
			//åºå
			out.printf("Case #%d: %s\n", i+1, ans);
		}
		out.flush();
	}
	
	
	/**
	 * éåãnåå·¦ã«ããããæ°ããéåãè¿ãã¾ãã
	 * @param in
	 * @param n
	 * @return
	 */
	private static char[] rotate(final char[] in, int n){
		if(n>=in.length){
			throw new IllegalArgumentException("n= " + n + ", in.length = "
					+ in.length + "ã¨ãªã£ã¦ãã¾ããn<in.lengthã«ãã¦ãã ããã");
		}
		char[] out = new char[in.length];
		
		//1,2,3,4ã2åå·¦ã«ãããå ´åãã¾ãå·¦å´ã®2åãéé¿ãã
		char[] tmp = new char[n];
		for(int i=0;i<n;i++){
			tmp[i] = in[i];
		}
		//æ¬¡ã«in[n]ããin[in.length]ã¾ã§ãout[0]ããout[in.length - 2]ã¾ã§ã«ã³ãã¼
		for(int i=0;i<in.length-n;i++){
			out[i] = in[i+n];
		}
		//æå¾ã«out[length-n,length]ã«tmp[0,n]ããå¤ããã©ã
		for(int i=in.length-n;i<in.length;i++){
			out[i] = tmp[i-in.length+n];
		}
		return out;
	}
}
