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.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

public class RecycledNumbers {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		File inDir = new File("RecycledNumbers/input");
		File outDir = new File("RecycledNumbers/output");
		File[] inFiles = inDir.listFiles();
		for (File inFile : inFiles) {
			BufferedReader br = new BufferedReader(new FileReader(inFile));
			PrintWriter pw = new PrintWriter(new File(outDir, inFile.getName()+".out"));
			long numLines = Long.valueOf(br.readLine());
			for (long i = 1; i <= numLines; i++) {
				Set<RecycledSet> recycledPairs = new HashSet<RecycledSet>();
				String[] limits = br.readLine().split(" ");
				long a = Long.valueOf(limits[0]);
				long b = Long.valueOf(limits[1]);
				long num = a;
				while (num <= b) {
					String numString = String.valueOf(num);
					int numDigits = numString.length();
					for (int idx = 0; idx < numDigits; idx++) {
						String recycledString = numString.substring(idx) + numString.substring(0, idx);
						System.out.println("Number: " + numString + " | Recycled: " + recycledString);
						long recycled = Long.valueOf(recycledString);
						if (recycled >= a && recycled <= b && num != recycled) {
							System.out.println("Adding");
							RecycledSet rs = new RecycledSet(num, recycled);
							recycledPairs.add(rs);
						}
					}
					num++;
				}
				System.out.println("Num Recycled Pairs: " + recycledPairs.size());
				pw.print("Case #"+i+": ");
				pw.println(recycledPairs.size());
			}
			br.close();
			pw.flush();
			pw.close();
		}
	}
}

class RecycledSet {
	
	long num1;
	long num2;

	public RecycledSet(long num1, long num2) {
		if (num1 < num2) {
			this.num1 = num1;
			this.num2 = num2;
		} else {
			this.num1 = num2;
			this.num2 = num1;
		}
	}
	
	public int hashCode() {
		return (int) (num1 & num2);
	}
	
	public boolean equals(Object o) {
		if (o instanceof RecycledSet) {
			RecycledSet rs = (RecycledSet) o;
			if (this.num1 == rs.num1 && this.num2 == rs.num2) {
				return true;
			}
		}
		return false;
	}
	
}
