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

public class recycled_numbers {
	public static void main(String[]a) throws Exception{
		
		Scanner f = new Scanner(new File(a[0]));
		int result=0;
		int A,B;
		String n,m;
		HashSet<String> s=new HashSet<String>();
		
		int T=f.nextInt();	//T total case count
		for (int t=1; t<=T;t++){//for each case t
			
			s.clear();
			result=0;				//reset
			A=f.nextInt();B=f.nextInt();	//get values for next case
			
			if (A==B)result=0;//remove case not meeting problem limits 
			else{
				for(int i=A;i<=B;i++){//for each int in range
					n=Integer.toString(i);
					m=new String(n);
					//System.out.println(n);
					for (int j=1;j<n.length();j++){//move each digit to the front & test
						m = m.substring(m.length()-1)+m.substring(0,m.length()-1);
						if(m.matches("^0+[\\d]+")!=true &&
								Integer.parseInt(m)<=B &&
								Integer.parseInt(n)<Integer.parseInt(m)){
							s.add(new String(n+","+m));
							//result++;
							//System.out.println(" matched: "+m);
						}
						
					}
				}
				result = s.size();
			}

			//display output
			System.out.println("Case #" + t + ": " + result);
		}
	}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class RecycledNumbers {
	
	public static int len(int A){
		int k = 1 ;
		int r = 0;
		while( k < A){
			k = k * 10;
			r = r + 1;
		}
		return r;
	}
	public static int count(int A , int B){
		int r = 0;
		for(int i = A ; i < B ; i ++){
			int mul = 1 ;
			int len = 0;
			while(mul <= i){
				mul = mul * 10;
				len = len + 1;
			}
			
			int[] prev = new int[len - 1];
			int it = 10;
			for(int j = 0 ; j < len - 1 ; j ++){
				int right = i % it;
				int left = i / it;
				mul = mul / 10;
				int result = right * mul + left;
				it = it * 10;
				
				boolean duplicate = false;
				for(int k = 0 ; k < j ; k ++){
					if(result == prev[k]){
						duplicate = true;
						break;
					}
				}
				if(result > i && result >= A && result <= B && !duplicate){
				//	System.out.println(result + "\t" + i);
					r ++;
					prev[j] = result;
				}
			}
		}
		return r;
	}
	
	public static void main(String args[]) throws IOException{
		//System.out.println(count(1 , 2000000));
		BufferedReader reader = new BufferedReader(new FileReader(new File("C-large.in")));
		BufferedWriter writer = new BufferedWriter(new FileWriter(new File("output.txt")));
		String line = reader.readLine();
		int T = Integer.parseInt(line);
		for(int i = 0 ; i < T ; i ++){
			line = reader.readLine();
			String tokens[] = line.split("\\s+");
			int N = Integer.parseInt(tokens[0]);
			int S = Integer.parseInt(tokens[1]);
			int count = count(N , S);
			writer.write("Case #" + (i + 1) + ": " + count + "\r\n");
		}
		reader.close();
		writer.close();
	}

}
