/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package recyclednumbers;

import java.util.HashSet;

/**
 *
 * @author vandit
 */
public class RecycleNumbers {

    private HashSet<String> numbers = new HashSet<String>();
    private HashSet<String> initialTempNumbers = new HashSet<String>();
    private HashSet<String> finalTempNumbers = new HashSet<String>();

    HashSet<Pair> numberPairs = new HashSet<Pair>();


    private void findRecycledNumbers(int start, int end, int places) {


        for (int i = start; i <= end; i++) {
            String initialNumber = Integer.toString(i);
            //if (!(numbers.contains(initialNumber) || finalTempNumbers.contains(initialNumber))) {

                StringBuffer tempNumber = new StringBuffer(initialNumber);
                int len = tempNumber.length();
                int startIndexToMove = len - places;
                String tempString = tempNumber.substring(startIndexToMove);
                if ((places == 1 && tempString.equals("0")) || tempString.charAt(0) == '0') {
                    continue;
                }
                tempNumber.delete(startIndexToMove, len);
                String finalTempNumber = tempString + tempNumber.toString();

                if (! (finalTempNumber.equals(initialNumber) || Integer.parseInt(finalTempNumber) > end || Integer.parseInt(finalTempNumber) < Integer.parseInt(initialNumber))) {
                    numbers.add(initialNumber);
                    finalTempNumbers.add(finalTempNumber);
                    Pair pair = new Pair(finalTempNumber,initialNumber);
                    numberPairs.add(pair);
                }
            }

        

    }

    public HashSet<Pair> findAllRecycledNumbers(int start, int end) {
        int length = Integer.toString(start).length();
        for (int i = 1; i < length; i++) {
            findRecycledNumbers(start, end, i);
        }
        return numberPairs;
    }
}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;


public class Prob3 {

	
	public static int calc(int n1, int n2){
		Set<String> set = new HashSet();
		int c=0;
		for(;n1<=n2;n1++){
			String sn = ""+n1;
			Set<String> tested = new HashSet();
			for(int i=0;i<sn.length();i++){				
				String s = sn.substring(i,sn.length())+sn.substring(0,i);
				if(s.charAt(0)=='0') continue;
				if(tested.contains(s)) continue; 
				tested.add(s);
				int s2 = Integer.parseInt(s);
				if(set.contains(s) && s2>=n1 && s2<=n2){
					c++;
				}else{
					set.add(s);
				}
			}
			
		}
		return c;
	}
	
	public static void main(String[] args) {
			
		try{

		  FileInputStream fstream = new FileInputStream("C-small-attempt2.in");

		  DataInputStream in = new DataInputStream(fstream);
		  BufferedReader br = new BufferedReader(new InputStreamReader(in));
		  String strLine;

		  FileWriter fstream1 = new FileWriter("C-small-attempt2.out");
		  BufferedWriter out = new BufferedWriter(fstream1);
//		  out.write("Hello Java");
		  //Close the output stream
		  
		  
		  long l = Long.parseLong(br.readLine());
		  for(long i=0; i<l && (strLine = br.readLine()) != null; i++)   {
			  String[] s = strLine.split(" "); 
			  int res = calc(Integer.parseInt(s[0]), Integer.parseInt(s[1]));
			  System.out.println("Case #"+(i+1)+": " +res);
			   out.write("Case #"+(i+1)+": " +res+"\n");
			  
			 // System.out.println("\n\n\n\n\n\n\n\n");
		  }			
		  out.close();
		} catch (Exception e1){
	    	e1.printStackTrace();
		}
	}
		
}
