/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package recyclednumbers;

import java.util.HashSet;

/**
 *
 * @author vandit
 */
public class RecycleNumbers {

    private HashSet<String> numbers = new HashSet<String>();
    private HashSet<String> initialTempNumbers = new HashSet<String>();
    private HashSet<String> finalTempNumbers = new HashSet<String>();

    HashSet<Pair> numberPairs = new HashSet<Pair>();


    private void findRecycledNumbers(int start, int end, int places) {


        for (int i = start; i <= end; i++) {
            String initialNumber = Integer.toString(i);
            //if (!(numbers.contains(initialNumber) || finalTempNumbers.contains(initialNumber))) {

                StringBuffer tempNumber = new StringBuffer(initialNumber);
                int len = tempNumber.length();
                int startIndexToMove = len - places;
                String tempString = tempNumber.substring(startIndexToMove);
                if ((places == 1 && tempString.equals("0")) || tempString.charAt(0) == '0') {
                    continue;
                }
                tempNumber.delete(startIndexToMove, len);
                String finalTempNumber = tempString + tempNumber.toString();

                if (! (finalTempNumber.equals(initialNumber) || Integer.parseInt(finalTempNumber) > end || Integer.parseInt(finalTempNumber) < Integer.parseInt(initialNumber))) {
                    numbers.add(initialNumber);
                    finalTempNumbers.add(finalTempNumber);
                    Pair pair = new Pair(finalTempNumber,initialNumber);
                    numberPairs.add(pair);
                }
            }

        

    }

    public HashSet<Pair> findAllRecycledNumbers(int start, int end) {
        int length = Integer.toString(start).length();
        for (int i = 1; i < length; i++) {
            findRecycledNumbers(start, end, i);
        }
        return numberPairs;
    }
}


import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
/**
 *
 * @author yilianz
 */
public class RecycleNumber{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception{

        // Read file
        Scanner inFile = new Scanner(new File("file.in"));
        PrintWriter outFile = new PrintWriter(new File("out.file"));

        int caseN =inFile.nextInt();


        // TODO code application logic here
        for(int i = 1; i<caseN+1; i++){

            int A = inFile.nextInt();
            int B = inFile.nextInt();
            int count =0;


            //Read the total point
            for(int n = A; n<=B; n++){

            	//check all the recycle of n to see whether it satisfies the condition
            	String nS = Integer.toString(n);
            	int l = nS.length();
            	int flag = 0 ;  //check whether m repeat
            	int[] c = new int[l];
            	for (int k=0; k<l;k++) c[k]=-1;

            	if (l==1) break;
            	for(int k=0;k<l;k++){
            		String mS=nS.substring(k+1,l)+nS.substring(0,k+1);
            		int m = Integer.parseInt(mS);
            		if (k==0) c[k]=m;
            		else{
            			for(int s = 0; s<k;s++){
            				if(m==c[s]) flag=1;
            			}
            		}
            		if (m >n && m<=B && flag==0){
            			count++;
            			//System.out.println(count);
            			//System.out.println("("+n+","+m+")");
            			c[k]=m;
            			flag=0;
            		}

            	}


            }

            System.out.println("Case #"+i+": "+count);
            outFile.println("Case #"+i+": "+count);
        }

        //close the file
         inFile.close();
         outFile.close();



       }
}
