/*
 * 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;
        } 
    
}


/*
 * @(#)CJ2012C.java
 * por Jorge LÃ³pez Palacios
 *
 * MÃ¡s informaciÃ³n y descripciÃ³n del CopyrightÂ©.
 *
 */

/**
 * Clase CJ2012C
 * @author Jorge LÃ³pez Palacios
 * @version 1.0
 */

//***************** PACKAGES ****************************//
package codejam;
//***************** IMPORTS *****************************//
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;


public class CJ2012C {
    //***************** CONSTANTES ****************************//
    private static final String SPATHFILEINPUT = "C:\\C-small-attempt0.in";
    private static final String SPATHFILEOUTPUT = "C:\\CJ2012C.txt";
    //***************** ATRIBUTOS *****************************//
    private static BufferedReader brBuffer;
    private static String sFileLine = "";
    private static PrintWriter pwFileOutput;
    //***************** METODOS SELECTORES ********************//

    //***************** METODOS MODIFICADORES *****************//

    //***************** CONSTRUCTORES *************************//

    //***************** MAIN **********************************//
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int iNumberCases = 0;
        int iCurrentLine = 0;

        int aiNumbers[] = new int[2];
        String asNumbers[] = new String[2];
        String sDummy = "";
        String sNNormal = "";
        //String sNBackward = "";
        int iCountRecycled = 0;
        //String sBackwardDummy = "";

        try{
            // Recogemos los datos del fichero
            brBuffer = getFileToString(SPATHFILEINPUT);

            // Recogemos el fichero de salida
            pwFileOutput = getFile();
            
            // Leemos cada lÃ­nea del fichero
            while((sFileLine = brBuffer.readLine()) != null){
                // Inicializo el contador de nÃºmeros reciclados
                iCountRecycled = 0;

                // Recojo el nÃºmero de casos
                if(iCurrentLine == 0){
                    iNumberCases = Integer.parseInt(sFileLine);
                }else{
                    // Recojo los nÃºmeros
                    asNumbers = sFileLine.split(" ");
                    aiNumbers = new int[asNumbers.length];

                    for(int i = 0; i <= asNumbers.length - 1; i ++){
                        aiNumbers[i] = Integer.parseInt(asNumbers[i]);
                    }

                    // Recojo todos los nÃºmeros de A hasta B
                    for(int n = aiNumbers[0]; n <= aiNumbers[1]; n ++){
                        // Recojo todos los nÃºmeros de B hasta A
                        for(int m = aiNumbers[1]; (m > n); m --){
                            // Compruebo si la combinacion (n, m) es de nÃºmeros reciclados
                            sNNormal = n + "";

                            for(int i = 0; (i <= sNNormal.length() - 1); i ++){
                                sDummy = sNNormal.substring((sNNormal.length() - i), sNNormal.length()) + sNNormal.substring(0, (sNNormal.length() - i));

                                if((Integer.parseInt(sDummy) == m) && ((Integer.parseInt(sDummy) + "").length() == (m + "").length())){
                                    iCountRecycled ++;
                                }
                            }
                        }
                    }
                }

                if(iCurrentLine == 0){
                    System.out.println("Numero de casos: " + iNumberCases);
                }else{
                    printToFile("Case #" + iCurrentLine + ": " + iCountRecycled);
                    //System.out.println("Case #" + iCurrentLine + ": " + iCountRecycled);
                }

                // Incrementamos el nÃºmero de lÃ­nea
                iCurrentLine ++;
            }
            //Cerramos el buffer del fichero
            closeInputStream(brBuffer);
        }catch (Throwable th){
            System.err.println("Error: " + th.toString());
        }
    }
    //***************** METODOS PÃBLICOS **********************//

    //***************** METODOS PRIVADOS **********************//
    /**
     * Devuelve los datos de un fichero indicado por parametro
     *
     * @param ai_sPathFile: Ruta del fichero
     * @throws IOException
     */
    private static BufferedReader getFileToString(String ai_sPathFile) throws IOException{
        // Abrimos el fichero
        FileInputStream fisFile = new FileInputStream(ai_sPathFile);

        // Recojo los datos del fichero
        DataInputStream disData = new DataInputStream(fisFile);
        brBuffer = new BufferedReader(new InputStreamReader(disData));

        return brBuffer;
    }

    /**
     * Cierra el buffer del fichero
     *
     * @param ai_bfrBuffer
     * @throws IOException
     */
    private static void closeInputStream(BufferedReader ai_bfrBuffer) throws IOException{
        if(ai_bfrBuffer != null){
            ai_bfrBuffer.close();
        }
    }

    /**
     * Recoge el fichero de salida en memoria
     *
     * @throws FileNotFoundException
     */
    private static PrintWriter getFile() throws FileNotFoundException{
	pwFileOutput = new PrintWriter(SPATHFILEOUTPUT);
	return pwFileOutput;
    }

    /**
     * Escribe en el fichero de salida
     *
     * @param ai_sText
     */
    public static void printToFile(String ai_sText){
        // Escribo en el fichero
        pwFileOutput.append(ai_sText);
        pwFileOutput.println();
        // Cierro el fichero
        pwFileOutput.flush();
    }

}//FIN DE LA CLASE