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

import java.util.HashMap;
import java.util.Scanner;

public class RecycledNumbers {

	//recycled numbers between A and B
	//OK, the key is being able to reduce each number to a single comparable value instead of comparing every possible match.
	//Let's say: We will take the permutation that has the smallest value.
	//creating them may be slow as long as looking them up is fast.

	//[not needed] Cache values to improve performance
	// perf test - worst possible case was solved in seconds.
	//we've only given 50 cases at most.
	//[DONE] what if more than two match? does that make 3 distinct recycled pairs?
	public static void main(final String[] args) {
		new RecycledNumbers().start();
	}

	private  void start() {
		final Scanner s = new Scanner(System.in);
		final int numTests = s.nextInt();
		for (int i = 0; i < numTests; i++) {
			//			deb("Case " + (i + 1) + "---------");
			final int startNum = s.nextInt();
			final int endNum = s.nextInt();
			final HashMap<String, Integer> values = new HashMap<String, Integer>();
			int distinctPairsCount = 0;
			for (int val = startNum; val <= endNum; val++) {
				final String newValue = findSmallestPermutation(""+val);
				if (values.containsKey(newValue)) {
					final int count = values.get(newValue);
					distinctPairsCount += count;
					values.put(newValue, count + 1);
					//					deb("found a match of " + count + ", total matches: " + distinctPairsCount);
				} else {
					values.put(newValue, 1);
				}
			}
			System.out.println("Case #" + (i+1) + ": " + distinctPairsCount);
		}
	}

	//We can't compare every permutation to every permutation
	//so we find the smallest and only use that.
	//this function looks slow, but it isn't called that often so should be OK.
	private String findSmallestPermutation(final String value) {
		int bestValue = Integer.MAX_VALUE;
		String bestString = null;
		final String valueTwice = value + value; //We will extract the substrings from this.
		for (int i = 0; i < value.length(); i++) {
			final String permutation = valueTwice.substring(i, value.length() + i);
			final int permutationValue = Integer.parseInt(permutation);
			if (permutationValue < bestValue) {
				bestValue = permutationValue;
				bestString = permutation;
			}
		}
		//		deb("source: " + value +", adding " + bestString);
		return bestString;
	}

	//	private void deb(final Object o) {
	//		System.out.println(o.toString());
	//	}

}
