/*
 * 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.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class ProblemC {
	private String line = "";
	private String fileContents = "";
	private String delim = "\r\n";
	private String fileName = "C-small-attempt0.in";
	
	public void solve() {
		StringTokenizer st = new StringTokenizer(fileContents,delim);
		StringTokenizer lineToken = null;
		int count = 1;
		int testCases = 0;
		int lower = 0;
		int	upper = 0;
		int y = 0;
		
		ArrayList<Integer> numbersList = new ArrayList<Integer>();
		
		testCases = Integer.parseInt(st.nextToken());
		
		while(st.hasMoreTokens()) {
			
			line = st.nextToken();
			
			System.out.print("Case #");
			System.out.print(count++);
			System.out.print(": ");
			
			lineToken = new StringTokenizer(line," ");
			
			lower = Integer.parseInt(lineToken.nextToken());
			upper = Integer.parseInt(lineToken.nextToken());

			y = 0;
			int numberLen = 0;
			for(int i=lower; i<upper; i++) {
				String number = String.valueOf(i);
				numberLen = number.length();
				for(int k = 1; k<numberLen; k++) {
					String newNumberBack = number.substring(0, numberLen-k);
					String newNumber = number.substring(numberLen-k);
					newNumber = newNumber + newNumberBack;
					if(!newNumber.startsWith("0") && newNumber.length() == number.length()) {
						Integer testNumber = Integer.parseInt(newNumber);
						if(lower <= i && i < testNumber && testNumber <= upper
//								&& !numbersList.contains(testNumber)
								) {
							numbersList.add(testNumber);
							y++;
							
/*							System.out.print("(");
							System.out.print(i);
							System.out.print(",");
							System.out.print(testNumber);
							System.out.print(")\t");
*/						}
					}
				}
			}
			System.out.println(y);
		}
	}
	
	public void readFile() {
		try {
			// Open the file that is the first
			// command line parameter
			FileInputStream fstream = new FileInputStream(fileName);
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			StringBuilder sb = new StringBuilder();
			// Read File Line By Line
			while ((strLine = br.readLine()) != null) {
				// Print the content on the console
				sb.append(strLine);
				sb.append(delim);
			}
			// Close the input stream
			in.close();
			
			fileContents = sb.toString();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}

}
