
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.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;


public class main {

	/**
	 * @param args
	 * @throws Exception 
	 */

	public static int[] flag;
	public static ArrayList[] array;
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		Input in = new Input("small.txt");
		
		File file = new File("out.txt");
		FileWriter filewriter = new FileWriter(file);
		BufferedWriter bw = new BufferedWriter(filewriter);
		PrintWriter pw = new PrintWriter(bw);
		
		int n = in.getInt();
		
		for(int i = 0; i < n ; i ++){
			int result = 0;
			int num = i + 1 ;
			int a = in.getInt();
			int b = in.getInt();	
			int d = Integer.toString(a).length();
			array = new ArrayList[b+1];
			for(int j = a ; j <= b; j++){
				array[j] = new ArrayList();
			}
			for(int j = a ; j <=b ; j++){
				String s = Integer.toString(j) + Integer.toString(j);
				result += solve(s,d,a,b,j);				
			}
			
			String ans = "Case #" +num +": "+result; 
			pw.println(ans);
			System.out.println(ans);
		}
		pw.close();
		bw.close();
		filewriter.close();
	}
	
	public static int solve(String s , int d,int a , int b,int j){
		int result = 0;
		int original = j;
		
		for(int i = 0 ; i < d ; i++){
			String sub = s.substring(i+1, i+1+d);
			int subint = Integer.parseInt(sub);
			
			if(subint >= a && subint <=b && subint !=original){
				if(subint<original){
					int flag = 0;
					for(int k  = 0 ; k < array[subint].size(); k++){
						if(Integer.parseInt(array[subint].get(k).toString())==original){
							flag++;
							break;
						}
					}
					if(flag ==0){
						array[subint].add(original);
						result++;
					}
				}else{
					int flag = 0;
					for(int k  = 0 ; k < array[original].size(); k++){
						if(Integer.parseInt(array[original].get(k).toString())==subint){
							flag++;
							break;
						}
					}
					if(flag ==0){
						array[original].add(subint);
						result++;
					}
				}
			}
			
		}
		return result;
	}

}
