
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class CodeJam {
static String [][] mapping = new String [6][];
 static String f,s,t,ff,ss,tt;
public static  void map(){
	f = "ejp mysljylc kd kxveddknmc re jsicpdrysi";
	s = "rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd";
	t = "de kr kd eoya kw aej tysr re ujdr lkgc jv";
	ff="our language is impossible to understand";
	ss="there are twenty six factorial possibilities";
	tt="so it is okay if you want to just give up";
	mapping[0] = f.split(" ");
	mapping[1] = ff.split(" ");
	mapping[2] = s.split(" ");
	mapping[3] = ss.split(" ");
	mapping[4] = t.split(" ");
	mapping[5] = tt.split(" ");
}
public static String returnLetter(String l){
	if(l.equals("q")) return "z";
	if(l.equals("z")) return "q";
	for(int i=0;i<6;i+=2){
		for(int j=0;j<mapping[i].length;j++){
			int n;
			if((n=mapping[i][j].indexOf(l))!=-1){
				return mapping [i+1][j].charAt(n)+"";
			}
		}
	}
	return "***";
}
public static boolean isRecycled(String p1, String p2){
	if(p1.length()!=p2.length()) return false;
	for(int i=0;i<p1.length();i++){
		String s = p1.substring((p1.length()-i-1));
		if(p2.indexOf(s)==0 && p2.substring(s.length()).equals(p1.substring(0, (p1.length()-i-1)))) {return true;}
	}
	return false;
}
public static String decode(String sen){
	String res = "";
	for(int i=0;i<sen.length();i++){
		if((sen.charAt(i)+"").equals(" ")) {res+=" ";continue;}
		res = res + returnLetter(sen.charAt(i)+"");
	}return res;
}
public static int enumerate(int low,int high){
	int c=0;
	for(int i=low;i<high;i++){
		for(int j=i+1;j<=high;j++){
		 if(isRecycled(i+"", j+"")) c++;
		}
	}
	return c;
}
public static void main(String [] areg) throws IOException{
	map();
	BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	int n=Integer.parseInt(bf.readLine());
	for(int i=0;i<n;i++){
		String sen = bf.readLine();
		String [] r = sen.split(" ");
		System.out.println("Case #"+((int)(i+1))+": "+enumerate(Integer.parseInt(r[0]), Integer.parseInt(r[1])));
		
	}
//	String s = bf.readLine();
//	String ss = bf.readLine();
//	System.out.println(enumerate(Integer.parseInt(s), Integer.parseInt(ss)));
	
}



}
