
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;

public class Recycle
{
	public static void main(String[] args)
	{
/*		Set<Integer> perms = getPermutations(123456);
		
		for(Integer i: perms)
			System.out.println(i);*/
	
		try
		{
			Scanner s = new Scanner(new File("recycle.in"));
			BufferedWriter w = new BufferedWriter(new FileWriter(new File("recycle.out")));
			int numCases = s.nextInt();
			s.nextLine();
			
			for(int n = 1;n <= numCases; n++)
			{
				int A = s.nextInt();
				int B = s.nextInt();
				int count = 0;
				for(int k = A; k <= B; k++)
				{
					count += getNumPairs(k, B);
				}
				
				w.write("Case #" + n + ": " + count + "\n");
			}
			
			w.flush();
			w.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public static int getNumPairs(int n, int B)
	{
		Set<Integer> possibles = getPermutations(n);
		int count = 0;
		for(Integer i : possibles)
			if(i > n && i <= B)
				count++;
		return count;
	}
	
	public static Set<Integer> getPermutations(int n)
	{
		Set<Integer> perms = new TreeSet<Integer>();
		char[] digits = String.valueOf(n).toCharArray();
		
		for(int firstPos = 1; firstPos < digits.length; firstPos++)
		{
			String toBuild = "";
			for(int k = 0; k < digits.length; k++)
				toBuild += digits[(firstPos + k) % digits.length];
			perms.add(Integer.parseInt(toBuild));
		}
		return perms;
	}
	

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

public class RecycledNumbers{

	public static void main(String[] args){

		String [] parse = new String[100];
		int T;
		String s = "";
		String a = "";
		int counter = 0;
		int count = 0;
		int pair = 0;
		int length = 0;
		int k = 0;
		int l = 0;
		int index = 0;
		int flag = 0;
		int [][] pairArry = new int [10000][2];
		File inFile = new File("C-small-attempt1.in");
		File outFile = new File("C-small-attempt1.txt");


		try{
			BufferedReader in = new BufferedReader(new FileReader(inFile));
			PrintWriter out = new PrintWriter(new FileWriter(outFile));

			T = Integer.parseInt(in.readLine());

			for(int i=0;i<T;i++){

				s = in.readLine();
				StringTokenizer st = new StringTokenizer(s, " ");
				while(st.hasMoreTokens()) {
					parse[counter++] = st.nextToken();
				}

				int min = Integer.parseInt(parse[0]);
				int max = Integer.parseInt(parse[1]);

				if((Integer.toString(max)).length() >= 2){

					for(int j=min; j<=max; j++){

						length = (Integer.toString(j)).length();

						s = Integer.toString(j);

						//out.println(s);

						a = "";

						for(k=0;k<length;k++){
							l = k;
							while(count < length){
								if(l >= length){
									l = l - length;
									a = a + Character.toString(s.charAt(l));
									l++;
								}else{
									a = a + Character.toString(s.charAt(l));
									l++;
								}

								count++;
							}
							//out.println(a);
							count = 0;

							if((j < Integer.parseInt(a)) && (Integer.parseInt(a) <= max)){
								pairArry[index][0] = j;
								pairArry[index][1] = Integer.parseInt(a);
								index++;
								pair++;
							}
							//count = 0;
							a = "";
						}
						//out.println();
					}
				}

				for(int m=0;m<index;m++){
					for(int n=m+1;n<index;n++){
						if(pairArry[m][0] == pairArry[n][0]){
							if(pairArry[m][1] == pairArry[n][1]){
								pair--;
							}
						}

						if(pairArry[m][0] == pairArry[n][1]){
							if(pairArry[m][1] == pairArry[n][0]){
								pair--;
							}
						}
					}
				}
				out.println("Case #" + (i+1) + ": " + pair);
				pairArry = new int [10000][2];
				pair = 0;
				counter = 0;
				index = 0;



			}

			in.close();
			out.close();
		}catch(IOException e){

			e.printStackTrace();
		}
	}
}


