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 exercise;


public class Solver {
	private static int[][]	MATCHES;
	
	public static Integer[]	solve(Integer[][] input) {
		Integer[]	result = new Integer[input.length];
		
		for (int cnt = 0; cnt < input.length; ++cnt)
			result[cnt] = solve(input[cnt]);
		
		return result;
	}

	public static int	solve(Integer[] input) {
		if (input == null || input.length < 2)
			return 0;
		
		Integer		min = input[0];
		Integer		max = input[1];
		int			result = 0;
		
		MATCHES = new int[max][4];
		
		for (int cnt = min; cnt < max; ++cnt) {
			result += countMatches(cnt, min, max);
		}
		
		for (int i = 0; i < MATCHES.length; ++i)
			for (int j = 0; j < MATCHES[i].length; ++j)
				if (MATCHES[i][j] != 0)
					++result;
				else
					break;
		return result;
	}
	
	private static int	countMatches(int value, int min, int max) {
		String	string = String.valueOf(value);
		int		result = 0;
		
		for (int idx = string.length() - 1; idx > 0; --idx) {
			String	candidateString = string.substring(idx, string.length()) + string.substring(0, idx);
			Integer	candidate = Integer.parseInt(candidateString);
			
			if (candidate.compareTo(value) > 0
					&& candidate.compareTo(min) >= 0
					&& candidate.compareTo(max) <= 0) {
				addToMatches(value, candidate);
			}
		}
		return result;
	}
	
	private static void	addToMatches(int value, int match) {
		boolean	inserted = false;
		for (int cnt = 0; cnt < MATCHES[value].length; ++cnt) {
			if (MATCHES[value][cnt] == 0) {
				MATCHES[value][cnt] = match;
				inserted = true;
				break;
			} else if (MATCHES[value][cnt] == match)
				return;
		}
		
		if (!inserted) {
			int[]	save = MATCHES[value];
			MATCHES[value] = new int[save.length * 2];
			
			int	cnt;
			for (cnt = 0; cnt < save.length; ++cnt) {
				MATCHES[value][cnt] = save[cnt];
			}
			MATCHES[value][cnt] = match;
		}
	}
}
