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 info.m3o.gcj2012.recyclednumber;

public class RecycledNumber {
	protected String numStr;
	protected int numDigits;

	protected int minA, maxB;

	public RecycledNumber(String s, int mina, int maxb){
		numStr = new String(s);
		numDigits = s.length();
		minA = mina;
		maxB = maxb;
	}

	//	public RecycledNumber(String s){
	//		numStr = new String(s);
	//		numDigits = s.length();
	//	}

	public RecycledNumber(int p, int mina, int maxb){
		numStr = new String(String.valueOf(p));
		numDigits = numStr.length();
		minA = mina;
		maxB = maxb;
	}

	//	public RecycledNumber(int p){
	//		numStr = new String(String.valueOf(p));
	//		numDigits = numStr.length();
	//	}

	public static String recycleAt(String s, int i){
		assert ((i>0) && (i<s.length())) : "int parameter is greater than length.";
		return ( new String (s.substring(i) + s.substring(0, i)));
	}

	public static String[] getCycledNumber(String s){
		String [] str;
		str = new String[s.length()-1];
		
		for(int i=1; i < s.length(); i++){
		 // str[i-1] = new String (recycleAt(s, i));
			str[i-1] = recycleAt(s, i);
		}
		return str;
	}
	

	public void addRecycledNumbers(MyNumberStringPairSet recycledNumberSet,
			MyNumberStringSet tested,  String sNum2) {
// TODO
		/*
		if (tested.contains(sNum2) == true){
			return;
		}*/

		int intS2 = Integer.valueOf(sNum2);

		if (((this.numStr.compareTo(sNum2)) != 0) && (sNum2.startsWith("0") == false)
				&& (intS2 >= this.minA) && (intS2 <= this.maxB)) {
			recycledNumberSet.add(new MyNumberStringPair(this.numStr, sNum2));
		}
		tested.add(numStr);
		tested.add(sNum2);

	}			


	
	
	public void addRecycledNumbers(MyNumberStringPairSet recycledNumberSet, MyNumberStringSet tested){
		if (tested.contains(numStr) == true){ // This value has been tested already.
			return;  
		}

		for(int i=1; i < this.numDigits; i++){
			String s = new String (recycleAt(numStr, i));
			
			// HERE TODO
			tested.add(s);
			int intS = Integer.valueOf(s);
			
			if ((s.startsWith("0")==false) 
					&& (numStr.compareTo(s) != 0)
					&& (intS >=this.minA) 
					&& (intS <= this.maxB)){
				recycledNumberSet.add(new MyNumberStringPair(s, numStr));
			}
			
//			if (s.startsWith("0")== false){
//				tested.add(s); // Here is the part
//			}
		}
		tested.add(numStr);		

	}



	public static boolean testrecycleAt(){
		boolean retb = false;
		boolean []b = new boolean[4];

		String str1 = "12345";

		String str2 = recycleAt(str1, 1);
		b[0] = ("23451".compareTo(str2)==0);

		str2 = recycleAt(str1, 2);
		b[1] = ("34512".compareTo(str2) == 0);

		str2 = recycleAt(str1, 3);
		b[2] = ("45123".compareTo(str2) == 0);

		str2 = recycleAt(str1, 4);
		b[3] = ("51234".compareTo(str2) == 0);

		retb = (b[0] && b[1] && b[2] && b[3]);
		assert (retb == true) : "something is wrong with recycleAt method.";
		return retb;
	}

	public String getNumStr() {
		return numStr;
	}
	public void setNumStr(String numStr) {
		this.numStr = numStr;
	}
	public int getNumDigits() {
		return numDigits;
	}
	public void setNumDigits(int numDigits) {
		this.numDigits = numDigits;
	}



}
