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

}

package QualificationRound2012.C.RecycledNumbers;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class RecycledNumbers {
	static final String _DIR = "src/QualificationRound2012/C/RecycledNumbers/";
	static final String INPUT_FILE = _DIR + "C-small-attempt0.in";
	static final String OUTPUT_FILE = INPUT_FILE + ".out";

	private static int algorithm(final String line) {
		int distinctPairs = 0;
		final String[] split = line.split(" ");
		final String A_str = split[0];
		final String B_str = split[1];
		final int A = Integer.parseInt(A_str);
		final int B = Integer.parseInt(B_str);

		// A â¤ n < m â¤ B
		for (int m = A; m <= B; m++) {
			for (int n = m + 1; n <= B; n++) {
				// System.out.print(A + " â¤ " + m + " < " + n + " â¤ " + B);
				if (isRecycled(m, n)) {
					// System.out.println(" --> YES!");
					distinctPairs++;
				} else {
					// System.out.println();
				}
			}
		}
		return distinctPairs;
	}

	private static boolean isRecycled(final int m, final int n) {
		final String mString = String.valueOf(m);
		final String nString = String.valueOf(n);
		final int length = mString.length();
		String rot = mString;
		for (int i = 0; i < length; i++) {
			if (rot.equals(nString)) {
				return true;
			}
			rot = rotate(rot);
		}
		return false;
	}

	public static void main(final String args[]) throws IOException {
		final BufferedWriter writer = new BufferedWriter(new FileWriter(OUTPUT_FILE));
		final BufferedReader reader = new BufferedReader(new FileReader(new File(INPUT_FILE)));

		long timeStart = System.currentTimeMillis();

		String line;
		int testcase = 0;
		while ((line = reader.readLine()) != null) {
			if (testcase != 0) {
				System.out.println("Case #" + testcase + ": " + algorithm(line));
			}
			testcase++;
		}

		long timeEnd = System.currentTimeMillis();
		System.out.println(timeEnd - timeStart + " millis");

	}

	private static String rotate(final String number) {
		final char[] numberArray = number.toCharArray();
		final char last = numberArray[numberArray.length - 1];
		return last + number.substring(0, number.length() - 1);
	}
}
