/*
 * 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.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;


public class RecycledNumbers {

	static class NumberPairs{
		int a ;
		int b ;
		int large ;
		int small;
		NumberPairs(String input){
			populateInput(input);
		}
		private void populateInput(String input) {
			StringTokenizer tokenizer = new StringTokenizer(input, " ");
			a = Integer.parseInt((String) tokenizer.nextElement());
			b = Integer.parseInt((String) tokenizer.nextElement());
			
			if(a<b || a==b){
				small = a;
				large = b;
			}else if (a>b){
				small = b;
				large = a;
			}
		}
		
		private int computeResult(){
			int count=0;
			
			for(int number = small ; number <= large; number ++){
				// Get every number under small
				Set<Integer> set = new HashSet<Integer>();
				for(int i = 0 ; i < Integer.toString(number).length()-1 ;i++){
					// Rotate number to get another number 
					
					int rotated = rotateCircular(number, i+1);
					
					if(number < rotated && rotated <= large){
						if(set.contains(rotated)) continue;
						else
							set.add(rotated);
						count++;
					}
				}
			}
			return count;
		}
		
		private static int rotateCircular(int number, int n){
			char [] val =rotateCircularly(Integer.toString(number), n);
			return Integer.parseInt(new String(val));
		}
		
		public void printResult(int i) {
			System.out.println("Case #" + i + ": " + computeResult());
		}
		
		
		public static  char[] rotateCircularly(String input1 , int n) {
			char[] input = input1.toCharArray();
			input = reverse (input, 0 , input.length-1);
			input = reverse (input , 0 , n-1);
			input = reverse (input, n , input.length-1);
			// Reverse the last n 
			
			return input;
		}
		
		public static char[] reverse(char[] input, int start, int end){
			for(int i = 0 ; i < (end - start + 2) / 2 ; i ++){
				char temp = input[start + i];
				input[start + i] = input[end-i];
				input[end-i] = temp;
			}
			return input;
		}
	}
	public static void main(String[] args) {
		try {
			String [] input = getInput("C-small-attempt0.in");
			for(int i=0;i< input.length; i++){
				new NumberPairs(input[i]).printResult(i+1);
			}
		} catch (IOException e) {
		}
		
	}
	
	static String[] getInput(String fileName) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(fileName));
		try {
			String message = reader.readLine();
			int numberOfTestCases = Integer.parseInt(message);
			String[] input = new String[numberOfTestCases];
			for (int i = 0; i < numberOfTestCases; i++) {
				message = reader.readLine();
				input[i] = message;
			}
			return input;
		} finally {
			reader.close();
		}
	}
}
