package codejam;

import java.util.*;
import java.io.*;

public class RecycledNumbers {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader("in.txt"));
		PrintWriter out = new PrintWriter(new File("out.txt"));
		
		int T = Integer.parseInt(in.readLine());
		for (int t = 1; t <= T; ++t) {
			String[] parts = in.readLine().split("[ ]+");
			int A = Integer.parseInt(parts[0]);
			int B = Integer.parseInt(parts[1]);
			
			int cnt = 0;
			for (int i = A; i < B; ++i) {
				String str = String.valueOf(i);
				int n = str.length();
				String res = "";
				Set<Integer> seen = new HashSet<Integer>();
				for (int j = n - 1; j > 0; --j) {
					res = str.substring(j) + str.substring(0, j);
					int k = Integer.parseInt(res);
					if (k > i && k <= B) {
						//System.out.println("(" + i + ", " + k + ")");
						if (!seen.contains(k)) {
							++cnt;
							seen.add(k);
						}
					}
				}
			}

			out.println("Case #" + t + ": " + cnt);
		}
		
		in.close();
		out.close();
		System.exit(0);
	}
}


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