/*
 * 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.*;
import java.util.*;

/**
 * */
public class Main {
	
	/**
	 * */
	public class TestCase{
		
		public Integer a, b;
		public Integer[] rNumbers; 
		public Integer iResult=0; 
		public Set<String> setPairs=Collections.synchronizedSet( new HashSet<String>());
		
		/**
		 * @param a
		 * @param b
		 * */
		public TestCase( int a, int b){
			this.a=a;
			this.b=b; 
			this.rNumbers=new Integer[ ( this.b- this.a)+ 1];
			for( int i=this.a; i<=this.b; i++)
				this.rNumbers[ i- this.a]=i; }
		
		/**
		 * */
		public void doSolution(){
			this.iResult=0;
			for( int i=0; i!=this.rNumbers.length; i++){
				Integer number=this.rNumbers[ i];
				String strNumber=String.valueOf( number); 
				if( strNumber.length()>=2){
					for( int s=1; s!=strNumber.length(); s++){
							String strPrefix=strNumber.substring( 0, s); 
							String strSuffix=strNumber.substring( s); 
							String strNewNumber=strSuffix+ strPrefix; 
							Integer newNumber=Integer.valueOf( strNewNumber);
							if(( newNumber>=this.a) && ( newNumber<=this.b) && ( number.compareTo( newNumber)!=0))
								this.addPair( number, newNumber); }}}
			this.iResult=this.setPairs.size(); }
		
		public void addPair( Integer n, Integer m){
			String nm=n+"+"+m; 
			String mn=m+"+"+n; 
			if( !this.setPairs.contains( nm) && !this.setPairs.contains( mn)){
				this.setPairs.add( nm); }}

	}
	public TestCase createTestCase( int a, int b){ return new TestCase( a, b); }	
	
	/**
	 * */
	public static void main( String[] args){

		try {
		
			Main m=new Main();
			LineNumberReader reader=new LineNumberReader( new FileReader( new File( args[ 0].trim())));
			PrintWriter printer=new PrintWriter( new FileWriter( args[ 1].trim()));
			
			int nTestCases=Integer.valueOf( reader.readLine());
			for( int i=0; i!=nTestCases; i++){
				String str[]=reader.readLine().split( " ", 2);
				TestCase testCase=m.createTestCase( Integer.valueOf( str[ 0].trim()), Integer.valueOf( str[ 1].trim())); 
				testCase.doSolution(); 				
				printer.println( "Case #"+( i+ 1)+": "+ testCase.iResult);
				System.out.println( "Case #"+( i+ 1)+": "+ testCase.iResult); }
						
			printer.close();
		
		} catch (FileNotFoundException e) {
		
		e.printStackTrace(); } catch (IOException e) {
		e.printStackTrace(); }}}
