/*
 * 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.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;


public class Recycle {

	public static void main(String[] args) throws IOException{
		FileInputStream inFile = new FileInputStream("C-small-attempt0.in");
		// Get the object of DataInputStream
		DataInputStream in = new DataInputStream(inFile);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String inputCnt = br.readLine();
		FileOutputStream outFile = new FileOutputStream("output.txt");
		DataOutputStream out = new DataOutputStream(outFile);
		BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(out));

		int cnt = Integer.parseInt(inputCnt);
		for(int line=1;line<=cnt;line++){
			String inputString = br.readLine();
			String[] splited = inputString.split(" ");
			int A = Integer.parseInt(splited[0]);
			int B = Integer.parseInt(splited[1]);
			String outputString = "Case #"+line+": ";
			int result = 0;
			for(Integer num = A; num<=B; num++){
				//for each number 
				String s = num.toString();
				result += permute(s,num,B);
			}
			outputString += result+"";
			wr.write(outputString);
			wr.newLine();
			wr.flush();

		}
		wr.close();
		out.close();
		br.close();
		in.close();
		inFile.close();
		outFile.close();
	}

	public static int permute(String original, int min, int max) {
		String target = original;
		HashSet<Integer> resultSet =  new HashSet<Integer>();
		for(int i = 1;i<original.length();i++){
			String shifted = target.charAt(target.length()-1)+"";
			shifted += target.substring(0,target.length()-1);
			int potential = Integer.parseInt(shifted);
			if(potential>min && potential <= max)
				resultSet.add(potential);
			target = shifted;
		}
		return resultSet.size();
	}
}
