/*
 * 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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class RecycledNumbersCodeJam2012Q {

	public long  solves(String A,String B){
		long a = Long.parseLong(A);
		long b = Long.parseLong(B);
		long s = 0 ; 
		for(long i = a ;i <= b; i++){
			s+= possibilities( Long.toString(i), a, b);
		}
		return s;
	}
	public long possibilities(String A,long a , long b ){
		long s = 0 ;
		long a0 = Long.parseLong(A);
		List<String> dup = new ArrayList<String>();
		dup.add(A);
		for(int i = 1 ; i < A.length();i++){
			String n= A.substring(i);
			String n1= A.substring(0,i);
			String aa = new String(n+n1);
			//System.out.println(aa);
			
			long a1= Long.parseLong(aa);
			
			if ( a1 < a0 &&  a1>= a && a1<=b && !dup.contains(aa)){	
				++s;
				dup.add(aa);
			}
		}
		return s ;
	}
	public static void main(String[] args) throws IOException {
		RecycledNumbersCodeJam2012Q si = new RecycledNumbersCodeJam2012Q();
	
		int T ;
		String CurLine = ""; 
	        
		InputStreamReader converter = new InputStreamReader(System.in);
		BufferedReader in = new BufferedReader(converter);
		CurLine = in.readLine();
		T= Integer.parseInt(CurLine);
		
		for(int i = 0 ; i < T;i++) {
			CurLine = in.readLine();
			String A = CurLine.split(" ")[0];
			String B = CurLine.split(" ")[1];
			System.out.println("Case #"+ (i+1)+": "+si.solves(A,B));
			
		}
	}
	
}
