import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Main {

//	static int MAX = 10000;
	static int MAX = 2000000;
	static Object[] All = new Object[MAX+1];
	
	static int size( int x ) {
		if(x>999999) return 7;
		if(x>99999)  return 6;
		if(x>9999)   return 5;
		if(x>999)    return 4;
		if(x>99)     return 3;
		if(x>9)      return 2;
		return 1;
	}
	static int[] rotate( int value ) {
		List<Integer> result = new ArrayList<Integer>();
		int[] V = new int[8];
		int s = size(value);
		int x = value;
		
		for(int i=s-1;i>=0;i--) {
			V[i] = x%10;
			x /=10;
		}
		int rot;
		for(int i=1; i<s; i++) {
			if(V[i]!=0){
				rot=0;
				for(int j=0,ii=i;j<s; j++,ii=(ii+1)%s){
					rot=rot*10+V[ii];
				}
				if(rot>value && !result.contains(rot))
					result.add(rot);
			}
		}
		if( result.isEmpty() )
			return null;
		int[] r = new int[result.size()];
		for(int i=0; i<r.length; i++)
			r[i]=result.get(i);
		return r;
	}
	
	static void precalculate() {
		for(int i=1; i<=MAX; i++){
			All[i] = rotate(i);
		}
	}
	static int solve( Scanner in ) {
		int A, B, sol=0;
		
		A = in.nextInt();
		B = in.nextInt();

		for( int i=A; i<=B; i++) {
//			List<Integer> result = rotate(i);
			if( All[i]!=null ) {
				for(int value: (int[])All[i] ) {
					if( value <= B )
						sol++;
				}
			}
		}

		return sol;
	}
	
	public static void main ( String args[] ) {
		int T; 
		Scanner in = new Scanner(System.in);
		T = in.nextInt();
		int cnt=0;
		int sol;
		precalculate();
		for( cnt=1; cnt<=T; cnt++ ) {
			sol = solve( in );
			System.out.printf("Case #%d: %d\n", cnt, sol);
		}

	}
}


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Scanner;

/**
 *
 * @author dafferianto
 */
public class RecycledNumbers {
    private static Scanner inputStream;
    private static PrintWriter outputStream;
    private static int T, lower, upper, counter, tempNumber, result;
    private static String numberString, tempNumberString;
    private static HashMap<Integer, Integer> aHashMap;
    public static void main(String[] args) throws FileNotFoundException, IOException {
        try {
            inputStream = new Scanner(new FileReader(args[0]));
            outputStream = new PrintWriter(new FileWriter("RecycledNumbersOutput.txt"));
            
            T = inputStream.nextInt();
            //T = Integer.parseInt(inputStream.nextLine());
            
            for (int i = 0; i < T; i++) { // For each test case
                lower = inputStream.nextInt();
                upper = inputStream.nextInt();
                aHashMap = new HashMap<Integer, Integer>();
                //result = solve(lower, upper);
                //outputStream.println("Case #" + (i+1) + ": " + result);
                outputStream.println("Case #" + (i+1) + ": " + solve(lower, upper));
                //System.out.println("Case #" + (i+1) + ": " + result);
            }
            
        } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
        }
    }

    private static int solve(int lower, int upper) {
        counter = 0;
        for (int currentNumber = lower; currentNumber <= upper; currentNumber++) {
            if (aHashMap.containsKey(currentNumber)) {
                continue;
            }
            counter += process(currentNumber);
        }
        return counter;
    }
    
    private static int process(int aNumber) {
        int localCounter = 0;
        numberString = Integer.toString(aNumber);
        for (int i = numberString.length()-1; i > 0; i--) {
            if (Character.getNumericValue(numberString.charAt(i)) == 0) {
                continue;
            }
            tempNumberString = numberString.substring(i) + numberString.substring(0, i);
            tempNumber = Integer.parseInt(tempNumberString);
            if (tempNumber == aNumber) {
                continue;
            }
            if (aHashMap.containsKey(tempNumber)) {
                continue;
            }
            if ((tempNumber >= lower) && (tempNumber <= upper)) {
                localCounter++;
                aHashMap.put(tempNumber, tempNumber);
                //System.out.println(tempNumber);
            }
        }
        if (localCounter != 0) {
            aHashMap.put(aNumber, aNumber);
            //System.out.println(aNumber);
        }
        if (localCounter > 1) {
            return sumOfSequence(localCounter);
        }
        return localCounter;
    }

    private static int sumOfSequence(int aCounter) {
        int tempSum = 0;
        for (int i = 1; i <= aCounter; i++) {
            tempSum += i;
        }
        return tempSum;
    }
}
