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.util.*;
import java.io.*;

public class Main {

	final static String FNAME = "A-small-practice";

	public BufferedReader in;

	public PrintWriter out;

	
	
	void open() throws IOException {
		in = new BufferedReader( new FileReader( new File( FNAME + ".in" ) ) );
		out = new PrintWriter( new File( FNAME + ".out" ) );
	}

	void close() throws IOException {
		out.close();
	}

	void run() throws IOException {
		
		
		String str = in.readLine();
		String[] input;
		int min,max,firstdigit, count;
		String newNum, converted;
		int num = new Integer( str );
		int[] array;
		for(int i = 0; i<num ; i++){
			
			input = in.readLine().split(" ");
			min = new Integer(input[0]);
			max = new Integer(input[1]);
			count=0;
			
			for(int j=min;j<=max; j++)
			{
				newNum=""+j;
				array = new int[input[0].length()-1];
				for(int k=1;k<newNum.length();k++)
				{
					boolean notsame = true;
					converted = newNum.substring(k) + newNum.substring(0,k);
					for(int h =0;h<k-1;h++)
					{
						if(new Integer(converted) == array[h] )
						{
							notsame = false;
						}
					}
					array[k-1] = new Integer(converted);
					if(new Integer(converted)>new Integer(newNum) && new Integer(converted)<=max && notsame)
					{
						count++;
					}
				}
			}
			
			out.println( "Case #" + (i+1) + ": "+ count );
			
		}
		
	}

	public static void main( String[] args ) throws IOException {
		new Thread() {

			public void run() {
				try {
					Main solution = new Main();
					solution.open();
					solution.run();
					solution.close();
				} catch ( Exception e ) {
					throw new RuntimeException( e );
				}
			}
		}.start();
	}
}