/*
 * 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.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;


public class C {

	Map<String, Set<Integer>> map = new HashMap<String, Set<Integer>>();

	public C() {
		for (int i=1; i<=2000000; i++) {
			char ch[] = (""+i).toCharArray();
			Arrays.sort(ch);
			String key = new String (ch);

			Set<Integer> val = map.containsKey(key)?map.get(key): new HashSet<Integer>();
			val.add(i);
			map.put(key, val);
		}

		System.out.println("Done..\n");
	}

	boolean rot (int a, int b) {
		String A = ""+a;
		String B = ""+b;
		for (int i=1; i<A.length(); i++) {
			String r = B.substring(i) + B.substring(0, i);
			//System.out.println(r);
			if (r.equals(A))
				return true;
		}
		return false;
	}
	
	int answer (int A, int B) {
		int total = 0;
		HashSet<String> stopList = new HashSet<String>();
		for (int i=A; i<=B; i++) {
			char ch[] = (""+i).toCharArray();
			Arrays.sort(ch);
			String key = new String (ch);

			if (stopList.contains(key))
				continue;
			Object values[] = map.get(key).toArray();
			for (int j=0; j<values.length; j++) {
				int valJ = (Integer)values[j];
				if (valJ>=A && valJ<=B) {
					for (int k=j+1; k<values.length; k++) {
						int valK = (Integer)values[k];
						if (valK>=A && valK<=B) {
							if (rot(valJ, valK)) {
								total++;
								//System.out.println(valJ+", "+valK);
							}
						}
					}

				}
			}
			stopList.add(key);


		}
		return total;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		C c = new C();
		
		//System.out.println(c.rot(12345, 45123));
		int T = sc.nextInt();

		for (int i=0; i<T; i++) {
			int A = sc.nextInt();
			int B = sc.nextInt();

			System.out.println("Case #"+(i+1)+": "+c.answer(A, B));
		}
	}
}
