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


public class C {
	public static final String INPUT_FILE = "C-small-attempt0.in";
	public static final String OUTPUT_FILE = "c1.out";

	public static void main(String[] args) throws FileNotFoundException {
		
		Scanner input = new Scanner(new File(INPUT_FILE));
		PrintStream output = new PrintStream(new File(OUTPUT_FILE));
		
		Map<Integer, Integer> answerKey = new HashMap<Integer, Integer>();
		answerKey.put(1,0);
		answerKey.put(2,1);
		answerKey.put(3,3);
		answerKey.put(4,6);
		answerKey.put(5,10);
		answerKey.put(6,15);
		answerKey.put(7,21);
		
		
		int trials = Integer.parseInt(input.nextLine());


		// START
		for(int i = 1; i <= trials; i++) {
			String[] line = input.nextLine().split(" ");
			int A = Integer.parseInt(line[0]); 
			int B = Integer.parseInt(line[1]);
			
			Set<Integer> seen = new HashSet<Integer>();
			List<Integer> answer = new ArrayList<Integer>();
			
			int length = getLength(A);
			
			if(length > 1) {
				for(int j = A; j <= B; j++) {
					
					if(!seen.contains(j)) {
					
						answer.add(numberOfPerms(A, B, j, length, seen));
					
					} // It is seen, so don't do anything
				}			
			} else { // length = 1			
			}			
			
			int answerCount = 0;
			for(int k = 0; k < answer.size(); k++) {
				answerCount += answerKey.get(answer.get(k));
			}
			
			output.print("Case #" + i + ": " + answerCount);
			if(i != trials) {
				output.println();
			}
	
		}
		
	}
	
	public static int numberOfPerms(int A, int B, int num, int length, Set<Integer> s) {
		int original = num;
		int count = 1;
		int permute = permute(num, length);
		s.add(original);
		while(original != permute) {
	
			if(permute >= A && permute <= B && !s.contains(permute)) {
				s.add(permute); // add bc we've now seen it
				count++;        // we have a good permute
			}
			
			permute = permute(permute, length);	
		}
		return count;
	}


	public static int getLength(int num) {
		return (num + "").length();

	}

	public static int permute(int num, int length) {
		int a = num % 10;
		int b = num / 10;
		return a * (int) Math.pow(10, length-1) + b;		
	}
	
}