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

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author daniele
 */
public class GCJ_C {
    
    public static void main(String[] args) throws Exception{
        
        
        
        Scanner in = new Scanner(new File(args[0]));
        FileWriter out = new FileWriter("/home/daniele/Scrivania/Output");
        

        int prove=in.nextInt();
        int min,max;
        int cifre;
        int cifreaus;
        String temp;
        int aus;
        int count=0;
        ArrayList<Integer> vals = new ArrayList<Integer>();
        int jcount=0;
        ArrayList<Integer> perm = new ArrayList<Integer>();
        
        out.write("");
        out.flush();
        
        for(int i=1;i<=prove;i++){
            
            out.append("Case #" +i+": ");out.flush();
            
            min=in.nextInt();
            max=in.nextInt();
            
            for(int j=min;j<=max;j++){
               if(!vals.contains(j)){
                    temp=""+j;
                    cifre=temp.length();
                    aus=j;
                    perm.add(j);
                    for(int k=1;k<cifre;k++){
                        aus=rotateOnce(aus);
                        temp=""+aus;
                        cifreaus=temp.length();//elusione zeri iniziali
                        if(aus>=min && aus<=max && aus!=j && cifreaus==cifre && nobody(vals,perm)){ 
                            perm.add(aus);
                            jcount ++;
                            
                        }
                        
                    }
                    while(jcount>0){count+=jcount; jcount--;}
                    vals.addAll(perm);
                    perm= new ArrayList<Integer>();
                    
                }
            }
            out.append(count+"\n");out.flush();
            count=0;
            vals= new ArrayList<Integer>();
        }
        
        
        
    }
        static public int rotateOnce(int n){
            String s=""+n;
            if(s.length()>1){
                s=s.charAt(s.length()-1) + s.substring(0,s.length()-1);
                n=Integer.parseInt(s);
            }
            return n;
        }    
        
        static public boolean nobody(ArrayList<Integer> v,ArrayList<Integer> a){;
            for(int i=0;i<a.size();i++)
                if(v.contains(a.get(i))) return false;
            return true;
        } 
    
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.HashSet;

public class ProblemB {

	static int A;
	static int B;
	static HashSet<Integer> set;

	public static void main(String[] args) {

		try {
			/* Reading */
			FileInputStream fstream = new FileInputStream("input.txt");
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));

			/* Writing */
			FileWriter fw = new FileWriter(new File("output"));
			BufferedWriter bw = new BufferedWriter(fw);

			String strLine;

			Integer T = 0;
			int caseCounter = 0;

			while ((strLine = br.readLine()) != null) {
				if (caseCounter == 0) {
					T = new Integer(strLine);
				} else {
					set = new HashSet<Integer>();
					int index =strLine.indexOf(' ');
					A = new Integer(strLine.substring(0, index).trim());
					B = new Integer(strLine.substring(index).trim());

					int nbRecycledPairs = 0;
					for (int i = A; i <= B; i++) {
						if (!set.contains(i)) {
							nbRecycledPairs += findRecycled(i);
						}
					}

					String toPrint;
					if (caseCounter == T) {
						toPrint = "Case #" + caseCounter + ": "
								+ nbRecycledPairs;
					} else {
						toPrint = "Case #" + caseCounter + ": "
								+ nbRecycledPairs + "\n";
					}
					System.out.print(toPrint);
					bw.write(toPrint);
				}
				caseCounter++;
			}
			in.close();
			bw.close();
		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}

	}

	static int findRecycled(int n) {
		String s = Integer.toString(n);
		int nbRecycled = 0;
		int nbRecycledPairs = 0;
		for (int i = 0; i < s.length(); i++) {
			String t = s.substring(i) + s.substring(0, i);
			if (t.charAt(0) != '0' && isInSet(t) && !t.equals(s)
					&& !set.contains(new Integer(t))) {
				set.add(new Integer(t));
				nbRecycled++;
			}

		}
		if (nbRecycled != 0) {
			nbRecycled++;
		}
		nbRecycledPairs = nbRecycled * (nbRecycled - 1) / 2;

		return nbRecycledPairs;
	}

	static boolean isInSet(String n) {
		int i = new Integer(n);
		if (i >= A && i <= B) {
			return true;
		}
		return false;
	}
}
