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.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;


public class RecycledNumber { 
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		
		File file = new File("/home/leandro/workspaces/googleCodeJam/RecycledNumbers/cases/C-small-attempt0.bin");
        BufferedReader br;
        br = new BufferedReader(new FileReader(file));
        int cases = Integer.parseInt(br.readLine());       
        for (int i = 1; i <= cases; i++) {
        	String line = br.readLine();
        	StringTokenizer tokenizer = new StringTokenizer(line);
        	Integer intA = Integer.parseInt(tokenizer.nextToken());
        	Integer intB = Integer.parseInt(tokenizer.nextToken());
        	int count = 0;
    		for (int n = intA; n <= intB; n++) {
    			for (Integer m : shift(n)) {
    				if(intA <= n && n < m && m <= intB){
    					//System.out.println(n + " - " + m);
    					count++;
    				}
    			}
    			
    		}
			System.out.println("Case #"+ i + ": " + count);
		}
		
		
		
		/*
		String A = "1111";
		String B = "2222";
		Integer intA = Integer.parseInt(A);
		Integer intB = Integer.parseInt(B);
		
		int count = 0;
		for (int n = intA; n <= intB; n++) {
			for (Integer m : shift(n)) {
				if(intA <= n && n < m && m <= intB){
					System.out.println(n + " - " + m);
					count++;
				}
			}
			
		}
		System.out.println("Total: "+ count);
		*/
	}
	
	private static Set<Integer> shift(int value){
		Set<Integer> retorno = new HashSet<Integer>();
		
		// length = 1 have no possibilities
		if(value < 10 ){
			return retorno;
		}
		String strValue = Integer.toString(value);
		
		String nextToAnalyze = strValue;
		for (int i = 1; i <= strValue.length()-1 ; i++) {
			char[] shifted = new char[nextToAnalyze.length()];
			for (int j = 0; j < nextToAnalyze.length(); j++) {
				if(j == 0){
					shifted[nextToAnalyze.length()-1] = nextToAnalyze.charAt(j);
				} else {
					shifted[j-1] = nextToAnalyze.charAt(j);
				}
			}
			nextToAnalyze = new String(shifted);
			// check for leading zeros
			if(nextToAnalyze.charAt(0) != '0'){
				retorno.add(Integer.parseInt(nextToAnalyze));
			}
		}
		return retorno;
	}
}
