/*
 * 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.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;


public class C {

	/**
	 * @param args
	 */
	static String n;
	static int count = 0;
	static String h;
	static int t ;
	static ArrayList<Integer> memo  = new ArrayList<Integer>();
	static ArrayList<Integer> old  = new ArrayList<Integer>();
	
	public static int solve(int s, int e){
		count = 0 ;
		memo.clear();
		old.clear();
		
		for (int i = s; i <= e; i++) {
			n = i+"";
			in:for (int j = 0; j < n.length(); j++) {
				
				h = n.substring(n.length()-j) + n.substring(0,n.length()-j);
				t = Integer.parseInt(h);
				
				if (t <= e && t>i ) {
					for (int k = 0; k < memo.size(); k++) {
						if (memo.get(k)==t && old.get(k)==i)
							continue in;
					}
					memo.add(t);
					old.add(i);
					count++;
				}
			}
			
		}		
		return count;
	}
	
	
	static BitSet v1  = new BitSet(2000000);
	static int [] index = new int[2000000];
	
	public static int solve2(int s, int e){
		count = 0;
		v1.clear();
		Arrays.fill(index, 0,0,e);

		for (int i = s; i <= e; i++) {
			n = i+"";
			for (int j = 0; j <= n.length(); j++) {
				
				h = n.substring(n.length()-j) + n.substring(0,n.length()-j);
				t = Integer.parseInt(h);

				if (t <= e && t >i ) {
						if (v1.get(i) && index[i]==t)
							continue;
						count++;
						v1.set(i);
						index[i] = t ;
				}
			}
		}
		
		return count;
	}
	
	
	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new FileReader("c.in"));
		BufferedWriter bw = new BufferedWriter(new FileWriter("c.out"));
		int n;
		int t = Integer.parseInt(br.readLine().trim());
		String ss[];
		
		for (int i = 1; i <= t; i++) {
		ss= br.readLine().split(" ");
		n = solve2(Integer.parseInt(ss[0]), Integer.parseInt(ss[1]));
//		System.out.println("Case #"+i+": "+n);
		bw.write("Case #"+i+": "+n);
		bw.newLine();
		}
		
	bw.close();
	}
}
