import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class CopyOfCopyOfMain {

	static int ndigits(int n) {
		return (int) (Math.log10(n) + 1);
	}

	static int rot(int n, int dig) {
		return (int) ((n % 10) * Math.pow(10, dig - 1)) + (int) (n / 10);
	}

	public static void main(String[] args) throws FileNotFoundException {
		String file = "C-large";
		Scanner in = new Scanner(new File(file + ".in"));
		PrintWriter out = new PrintWriter(new File(file + ".out"));

		int T = in.nextInt();
		for (int t = 1; t <= T; t++) {
			int A = in.nextInt();
			int B = in.nextInt();

			int c = 0;
			int[] list = new int[ndigits(B)];
			numbers: for (int i = A; i <= B; i++) {
				int digs = ndigits(i);
				int rot = i;
				int pairs = 0;

				Arrays.fill(list, -1);
				list[0] = i;

				digits: for (int j = 1; j <= digs + 1; j++) {

					rot = rot(rot, digs);
					if(rot < i)
						continue;

					if (A <= rot && rot <= B) {
						for (int k = 0; k < j; k++) {
							if (list[k] == rot) {
								continue digits;
							}
						}
						pairs++;
						list[j] = rot;
					}
				}

				//c += pairs * (pairs + 1) / 2;
				c += pairs ;
				// c+=digs;
			}
			out.println("Case #" + t + ": " + c);
		}

		in.close();
		out.close();
	}
}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class C {

	
	private void run() throws IOException {
		// put your code here ..
		
		int testcases=nextInt();
		
		int number=1;
		
		

		
		
		while(testcases-->0){
			long answer=0;
			
			int A=nextInt();
			int B=nextInt();
			

			
			

			for (int i = A; i < B; i++) {
				String a=new String(i+"");
				ArrayList<String> hash=new ArrayList<String>();
				for (int j = 1; j < a.length(); j++) {
					String now =a.charAt(a.length()-1)+a.substring(0,a.length()-1);
					int n=Integer.parseInt(now);
					String nlen=n+"";
					
					if(nlen.length()!=now.length()||nlen.equals(a)||n<=i||hash.contains(n+"")){
						a=now;
						continue;
					}
					

					if(n<=B){
						hash.add(n+"");
						answer++;
						
					}
					a=now;
					
				}
				
			}

				
			
			writeln("Case #"+(number++)+": "+answer);
		}
		
		
	}
	
	
	
	
	
	
	
	

	private int nextInt() throws IOException {
		if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
		return Integer.parseInt(input.nextToken());
	}
	private long nextLong() throws IOException {
		if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
		return Long.parseLong(input.nextToken());
	}
	private double nextDouble() throws IOException {
		if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
		return Double.parseDouble(input.nextToken());
	}
	private String nextString() throws IOException {
		if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
		return input.nextToken();
	}
	private void write(String output){
		out.write(output);
		out.flush();
	}
	private void writeln(String output){
		out.write(output+"\n");
//		out.flush();
	}
	private void end() throws IOException{
		in.close();
		out.close();
		System.exit(0);
	}
	
	private BufferedReader in;
	private PrintWriter out;
	private StringTokenizer input;
	
	public C() throws IOException {
		//Input Output for Console to be used for trying the test samples of the problem 
//		in = new BufferedReader(new InputStreamReader(System.in));
//		out = new PrintWriter(System.out);
		//-------------------------------------------------------------------------
		//Input Output for File to be used for solving the small and large test files
		in = new BufferedReader(new FileReader("C-large.in"));
		out = new PrintWriter("output3Large.txt");
		//-------------------------------------------------------------------------
		//Initialize Stringtokenizer input
		input = new StringTokenizer(in.readLine());
	}
	
	public static void main(String[] args) throws Exception {
		//-------------------------------------------------------------------------
		//initializing...
		C sol = new C();
		//-------------------------------------------------------------------------
		sol.run();
		//-------------------------------------------------------------------------
		//closing up
		sol.end();
		//--------------------------------------------------------------------------
	}
	
}
