package codejam;

import java.util.*;
import java.io.*;

public class RecycledNumbers {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader("in.txt"));
		PrintWriter out = new PrintWriter(new File("out.txt"));
		
		int T = Integer.parseInt(in.readLine());
		for (int t = 1; t <= T; ++t) {
			String[] parts = in.readLine().split("[ ]+");
			int A = Integer.parseInt(parts[0]);
			int B = Integer.parseInt(parts[1]);
			
			int cnt = 0;
			for (int i = A; i < B; ++i) {
				String str = String.valueOf(i);
				int n = str.length();
				String res = "";
				Set<Integer> seen = new HashSet<Integer>();
				for (int j = n - 1; j > 0; --j) {
					res = str.substring(j) + str.substring(0, j);
					int k = Integer.parseInt(res);
					if (k > i && k <= B) {
						//System.out.println("(" + i + ", " + k + ")");
						if (!seen.contains(k)) {
							++cnt;
							seen.add(k);
						}
					}
				}
			}

			out.println("Case #" + t + ": " + cnt);
		}
		
		in.close();
		out.close();
		System.exit(0);
	}
}

import java.io.*;
import java.util.StringTokenizer;

public class RecycledNumbers {
	public static void main(String[] args) {
		try {
			BufferedReader ifStream = new BufferedReader(new FileReader("C.in"));
			PrintWriter ofStream = new PrintWriter(new FileWriter("C.out"));
			int numCase;
			String inputCase;

			numCase = Integer.parseInt(ifStream.readLine());
			for (int i=1; i<=numCase; i++) {
				ofStream.println("Case #"+i+": "+recycledNumbers(ifStream.readLine()));
			}

			ifStream.close();
			ofStream.close();
		}
		catch (IOException e) {
			System.out.println("IOException.");
		}
	}

	private static int  recycledNumbers(String input) {
		StringTokenizer st = new StringTokenizer(input, " ", false);
		int A = Integer.parseInt(st.nextToken());
		int B = Integer.parseInt(st.nextToken());
		int numDigits = String.valueOf(A).length();
		int[] mArr = new int[numDigits];
		int count, m;
		int result = 0;

		for (int n=A; n<B; n++) {
			count = 0;
			for (int i=1; i<numDigits; i++) {
				m = shiftInt(n, i);
				if (m > n && m <= B) {
					mArr[count++] = m;
				}
			}
			result += count;
			for (int i=0; i<count; i++) {	// redundancy check
				for (int j=i+1; j<count; j++) {
					if (mArr[j] == mArr[i]) {
						result--;
						mArr[j] = 0;
					}
				}
			}
		}

		return result;
	}

	private static int shiftInt(int num, int k) {
		String before = String.valueOf(num);
		String after = before.substring(k, before.length()) + before.substring(0, k);

		return Integer.parseInt(after);
	}
}
