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();
	}
}

package codejam;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.HashSet;

public class _Q_C {
	public static void main(String[] args) throws Exception {
		String input_file = "C-large.in";
		String output_file = "_Q_C_large_out.txt";
	
		BufferedReader in = new BufferedReader(new FileReader(input_file));
		PrintStream out = new PrintStream(new FileOutputStream(output_file));
		
		in.readLine();
		String line;
		int case_num = 1;
		while ((line=in.readLine()) != null) {  
			String[] line_str = line.split(" ");
			int a = Integer.parseInt(line_str[0]);
			int b = Integer.parseInt(line_str[1]);
			
			HashSet<String> hs = new HashSet<String>();
			for (Integer n = a; n <= b; ++n) {
				String n_str = n.toString();
				int len = n_str.length();
				for (int r = 1; r < len; ++r) {
					String rotated_n_str =
						n_str.substring(len - r) + n_str.substring(0, len - r);
					if (isValidPair(a, b, n_str, rotated_n_str)) {						
						String check_str =
							removeLeadingZero(n_str) + "_" +
							removeLeadingZero(rotated_n_str);
						String check_str_rev =
							removeLeadingZero(rotated_n_str) + "_" +
							removeLeadingZero(n_str);
						if (!hs.contains(check_str) &&
								!hs.contains(check_str_rev)) {
							int rotated_n =
								Integer.parseInt(rotated_n_str);
							if (n <= rotated_n)
								hs.add(check_str);
							else
								hs.add(check_str_rev);
						}						
					}
				}
			}
						
			//System.out.println("Case #" + case_num + ": " + hs.size() + "\n" + hs.toString());
			out.println("Case #" + case_num + ": " + hs.size());
			++case_num;
		}
	}
	
	private static boolean isValidPair(int a, int b, String n_str, String m_str) {		
		Integer n = Integer.parseInt(n_str);
		Integer m = Integer.parseInt(m_str);
		
		String n_str_strip = n.toString();
		String m_str_strip = m.toString();
		
		return (n_str_strip.length() == m_str_strip.length() &&
				a <= n && n < m && m <= b);		
	}
	
	private static String removeLeadingZero(String s) {
		int i = 0;
		for (; i < s.length(); ++i) {
			if (s.charAt(i) != '0')
				break;
		}
		return s.substring(i);
	}
}
