import java.util.*;
import java.io.*;

public class recycled_numbers {
	public static void main(String[]a) throws Exception{
		
		Scanner f = new Scanner(new File(a[0]));
		int result=0;
		int A,B;
		String n,m;
		HashSet<String> s=new HashSet<String>();
		
		int T=f.nextInt();	//T total case count
		for (int t=1; t<=T;t++){//for each case t
			
			s.clear();
			result=0;				//reset
			A=f.nextInt();B=f.nextInt();	//get values for next case
			
			if (A==B)result=0;//remove case not meeting problem limits 
			else{
				for(int i=A;i<=B;i++){//for each int in range
					n=Integer.toString(i);
					m=new String(n);
					//System.out.println(n);
					for (int j=1;j<n.length();j++){//move each digit to the front & test
						m = m.substring(m.length()-1)+m.substring(0,m.length()-1);
						if(m.matches("^0+[\\d]+")!=true &&
								Integer.parseInt(m)<=B &&
								Integer.parseInt(n)<Integer.parseInt(m)){
							s.add(new String(n+","+m));
							//result++;
							//System.out.println(" matched: "+m);
						}
						
					}
				}
				result = s.size();
			}

			//display output
			System.out.println("Case #" + t + ": " + result);
		}
	}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.Arrays;

/**
 * Recycled Numbers
 * @author shibink
 *
 */
public class Recycle {

    public static int getCount(String str) {
        int count = 0;
        long tmp = 0;
        int numDigits = 0;

        if ((str == null) || (str.isEmpty())) {
            return count;
        }

        String[] strArray = str.split(" ");
        
        // It should be 2
        if (strArray.length != 2) {
            return 0;
        }
        long num1 = Long.parseLong(strArray[0]);
        long num2 = Long.parseLong(strArray[1]);
        tmp = num1;

        // Find number of digits in num1 & num2
        while (tmp > 0) {
            numDigits++;
            tmp = tmp / 10;
        }
        
        long arr[] = new long[numDigits];
        int cnt;
        for (long i = num1; i <= num2; i++) {
            Arrays.fill(arr, 0);
            cnt = 0;
            for (long val = i, j = 1; j < numDigits; j++) {
                tmp = val % 10;

                if (tmp == 0) {
                    val = val/10;
                    continue;
                }
                val = (int) (tmp * Math.pow(10, (numDigits - 1)) + (val / 10));
                for (int k=0; k<cnt; k++) {
                    if (val == arr[cnt]) {
                        continue;
                    }
                }
                arr[cnt++] = tmp;
                if ((val > i) && (val <= num2)) {
                    count++;
                }
            }
        }
        return count;
    }

    public static void processFile(String fileName) throws IOException {
        FileInputStream fstream = new FileInputStream(fileName);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        // use buffering
        Writer output = new BufferedWriter(new FileWriter("output.txt"));

        try {
            String strLine = null;
            int numRecycle = 0;
            int count = 0;

            if ((strLine = br.readLine()) != null) {
                count = Integer.parseInt(strLine);
            }
            // Read File Line By Line
            for (int i = 1; i <= count; i++) {
                if ((strLine = br.readLine()) == null) {
                    return;
                }

                numRecycle = getCount(strLine);
                System.out.println("Case #" + i + ": " + numRecycle);

                StringBuffer outBuff = new StringBuffer("Case #" + i + ": " + numRecycle + "\n");
                output.write(outBuff.toString());
            }

        } finally {
            // Close the input stream
            in.close();
            output.close();
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String fileName = "input.in";
        try {
            processFile(fileName);
        } catch (IOException e) {
            // TODO Log error
        }
        return;
    }
}
