import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class Recycle {

	private static HashMap<Integer, HashSet<Integer>> map = new HashMap<Integer, HashSet<Integer>>();
	private static HashSet<Integer> toSkip = new HashSet<Integer>();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int T = reader.nextInt();
		for (int tc = 1; tc <= T; tc++) {
			int a = reader.nextInt();
			int b = reader.nextInt();
			long before = System.currentTimeMillis();
			int res = doit(a, b);
			System.out.printf("Case #%d: %d\n", tc, res);
			long after = System.currentTimeMillis();
//			System.out.println("time taken: " + (after - before));
		}
	}

	private static int doit(int a, int b) {
		int count = 0;
		HashSet<Integer> skip = new HashSet<Integer>(toSkip);
		for (int i = a; i <= b; i++) {
			if (skip.contains(i))
				continue;
			HashSet<Integer> arr = generate(i);
//			if(arr.size() > 1) {
//				map.put(i, arr);
//			}
			// System.out.println(i + " --> " +
			// Arrays.deepToString(arr.toArray()));
			int temp = 0;
			if (arr.size() > 1) {
				for (int x : arr) {
					if (x >= a && x <= b) {
						temp++;
						skip.add(x);
					}
				}
				count += temp * (temp - 1) / 2;
			}
//			System.out.println("not skipping " + i + " gen: " + arr.size()+ " " + (temp * (temp - 1) / 2));
		}
		return count;
	}

	private static HashSet<Integer> generate(int num) {
		if(map.containsKey(num)) {
			HashSet<Integer> list = map.get(num);
			return list;
		}
		HashSet<Integer> list = new HashSet<Integer>();
		String s = "" + num;
		list.add(num);
		int len = s.length();
		for (int i = 1; i < len; i++) {
			String temp = s.substring(i) + s.substring(0, i);
			// System.out.println("temp: " + temp);
			if (temp.charAt(0) == '0')
				continue;
			int x = Integer.parseInt(temp);
			if( x <= 2000000) {
				list.add(x);
			}
		}
		if(list.size() > 1) {
			map.put(num, list);
		} else {
			toSkip.add(num);
		}
		return list;
	}

}

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;



public class RecycledNum {

	
	public static void main(String[] args) throws IOException
	{
		File file = new File("C-small-attempt0.in");
		Scanner scanner;
		BufferedWriter out = new BufferedWriter(new FileWriter("outfile1"));
		
		try
		{
			scanner = new Scanner(file);
			int testCases = scanner.nextInt();
			scanner.nextLine();  
			
			for(int numTest=0; numTest<testCases; numTest++)
			{
				int A = scanner.nextInt();
				int B = scanner.nextInt();
				scanner.nextLine();
				int totalcount=0;
				
				Stack<Integer> st = new Stack<Integer>();
				int numDigit =0;
				int temp = A;
				while(temp != 0)
				{
					st.push(temp%10);
					numDigit++;
					temp=temp/10;	
				}
				int[] digitsA = new int[numDigit];
				for(int i=0; i<numDigit; i++)
					digitsA[i]= st.pop();
				int[] digitsB = new int[numDigit];
				temp=B;
				int index=numDigit;
				while(temp != 0)
				{
					digitsB[index-1]=(temp%10);
					index--;
					temp=temp/10;	
				}
				
				
				
				int numofInt = B-A+1;
				boolean[] mark = new boolean[numofInt];
			
				
				for(int num=A; num<=B; num++)
				{
				
					int[] curDigits = new int[numDigit];
					temp = num;
				    index=numDigit;
					while(temp!=0)
					{
						curDigits[index-1]=(temp%10);
						index--;
						temp=temp/10;	
					}
					int count = 0;
					for(int j=0; j<numDigit; j++)
					{
						boolean cycle = false;
						if(curDigits[j]!=0)
						{
							int sum=0;
							int curindex=j;
							for(int kk=0; kk<numDigit; kk++)
							{
						
					
								if(curindex>=numDigit)
								
									sum=sum*10+curDigits[curindex-numDigit];
								else
									sum=sum*10+curDigits[curindex];
								curindex=curindex+1;
								
							}
				
							if((sum<=B)&&(sum>=A))
								cycle=true;
							
							
							
							if(cycle==true)
							{
								
							
								
								if(mark[sum-A]!= true)
								{
									mark[sum-A]= true;
									count++;
				
								}	
							}
							
						}
					}
					
					if(count==7)
						totalcount=totalcount+21;
					else if(count==6)
						totalcount=totalcount+15;
					else if(count==5)
						totalcount=totalcount+10;
					else if(count==4)
						totalcount=totalcount+6;
					else if(count==3)
						totalcount=totalcount+count;
					else if(count==2)
						totalcount=totalcount+1;
					
				}
				
				
				
				out.write("Case #"+(numTest+1)+": "+ totalcount +"\n");
				
			}
			out.close();
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}
}
