/*
 * 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.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

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

/**
 *
 * @author Ari
 */
public class ProblemC {
    static String in = "src/c.in";
    static String out = "src/c.out";
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Scanner scan = new Scanner(new File(in));
        BufferedWriter write = new BufferedWriter(new FileWriter(out));
        int numlines = scan.nextInt();
        String mini;
        String maxi;
        byte[] min = new byte[7];
        byte[] max = new byte[7];
        byte[] iter = new byte[7];
        int numDigits;
        int numPossible;
        int answer;
        int[] rots = new int[7];
        byte currentRots;
        byte compare = 0;
        
        //Loop vars:
        int x;
        int y;
        int d;
        int m;
        int n;
        int rot;
        int count;
        
        for(x = 0; x < numlines; x++) {
            write.write("Case #" + (x+1) + ": ");
            mini = scan.next();
            maxi = scan.next();
            answer = 0;
            
            numPossible = Integer.parseInt(maxi) - Integer.parseInt(mini);
            numDigits = mini.length();
            
            for(y = 0; y < numDigits; y++) {
                min[y] = Byte.parseByte(""+mini.charAt(y));
                iter[y] = min[y];
                max[y] = Byte.parseByte(""+maxi.charAt(y));
            }
            
            for(count = 0; count < numPossible; count++) {
                currentRots = 0;
                for(rot = 1; rot < numDigits; rot++) {
                    compare = 0;
                    for(d = 0; d < numDigits - 1; d++) {
                        compare = iter[(d + rot) % numDigits];
                        if(compare > iter[d]) {
                            compare = -2;
                            break;
                        }
                        else if(compare < iter[d]) {
                            compare = -1;
                            break;
                        }
                    }
                    if(compare > -1) {
                        if(iter[(numDigits + rot - 1) % numDigits] > iter[numDigits - 1]) {
                            compare = -2;
                        }
                    }
                    if(compare == -2) {//rotation is greater
                        for(d = 0; d < numDigits; d++) {
                            compare = iter[(d + rot) % numDigits];
                            if(compare > min[d]) {
                                compare = 1;
                                break;
                            }
                            else if(compare < min[d]) {
                                compare = -1;
                                break;
                            }
                        }
                        if(compare > -1) {//>=min
                            for(d = 0; d < numDigits; d++) {
                                compare = iter[(d + rot) % numDigits];
                                if(compare > max[d]) {
                                    compare = -1;
                                    break;
                                }
                                else if(compare < max[d]) {
                                    compare = 1;
                                    break;
                                }
                            }
                            if(compare > -1) {//<=max
                                rots[currentRots] = rot;
                                currentRots++;
                                answer++;
                            }
                        }
                    }
                }
                
                for(n = 0; n < currentRots-1; n++) {
                    for(m = n+1; m < currentRots; m++) {
                        for(d = 0; d < numDigits; d++) {
                            compare = iter[(d + rots[n]) % numDigits];
                            compare -= iter[(d + rots[m]) % numDigits];
                            if(compare != 0) {
                                compare = 1;
                                break;
                            }
                        }
                        if(compare == 0) {
                            answer--;
                            break;
                        }
                    }
                }
                
                for(d = numDigits-1; d >= 0; d--) {
                    if(iter[d] == 9) {
                        iter[d] = 0;
                    }
                    else {
                        iter[d]++;
                        break;
                    }
                }
            }
            
            write.write("" + answer);
            write.write('\n');
        }
        scan.close();
        write.close();
    }

    private static void print(byte[] iter, int rot) {
        for(int d = 0; d < iter.length; d++) {
            System.out.print(""+iter[(d + rot) % iter.length]);
        }
    }
    
    private static int get(byte[] iter, int rot, int length) {
        String s = "";
        for(int d = 0; d < length; d++) {
            s+=iter[(d + rot) % length];
        }
        return Integer.parseInt(s);
    }
}
