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.*;
import java.util.StringTokenizer;
import java.lang.Math;

public class qualifycodejamC{

	public static int checkNum(int num, int m, int n){
			String snum = Integer.toString(num);
			int[] array = new int[snum.length()];
			int min = num;
			int count = 0;

			for(int i=0;i<array.length;i++){
				boolean dupe = false;
				char[] temp = new char[snum.length()];
				for(int j=0;j<array.length;j++){
					temp[j] = snum.charAt((j + i) % snum.length());


				}
				array[i] = Integer.parseInt(new String(temp));
				//do a dupe check
				for(int k=0;k<i;k++){
					if(array[k] == array[i]){
						dupe = true;
					}
				}
				//System.out.print(";"+array[i]+";");

				if (m <= array[i] && array[i] <= n && array[i] != num && !dupe){
					count++;

				}
				if(Math.min(min,array[i]) >= m){
					min = (int)Math.min(min,array[i]);
				}
				//System.out.println("length: "+array.length+" current: "+num+"; min: "+ min);
			}

			if(min != num){
				//System.out.println();
				return 0;
			}
			//System.out.println("!X count: "+ count);
			if(count == 2){
				return 3;
			}else if(count == 3){
				return 6;
			}else if(count == 4){
				return 10;
			}else if(count == 5){
				return 16;
			}else if(count == 6){
				return 21;
			}else if(count == 7){
				return 28;
			}else if(count == 8){
				return 36;
			}

			return count;
	}

	public static void main(String[] args){

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		   String x = null;
		   String linein = null;
		   int totalcases = 0;
		   int result = 0;
		   int n = 0;
		   int m = 0;
		   int counter = 1;


		   try{
		    x = br.readLine();
		    totalcases = Integer.parseInt(x);

		    while( (linein = br.readLine()) != null){
					result = 0;

					StringTokenizer st = new StringTokenizer(linein);
					n = Integer.parseInt(st.nextToken());
					m = Integer.parseInt(st.nextToken());

					for (int i=n;i<m+1;i++){
						result = result + checkNum(i,n,m);
					}

					System.out.print("Case #" + (counter) + ": "+ result);
					//System.out.println(" debug: return val: "+process_return+"; used surprises: " + surprise_count + "/"+ttl_surprises);
					System.out.println();

					counter++;
				}

		   }
		   catch (IOException e) {
			   e.printStackTrace();
		   }

	}

}