/*
 * 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.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;


public class recycle {
	public static void main(String[] args) {
		int lines;
		String input;
		StringTokenizer strk;
		int start=1111;
		int end=2222;
		int count;
		
		try {
			BufferedReader inputStream = new BufferedReader(new FileReader("C-large.in"));
			BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
			
			lines = Integer.parseInt(inputStream.readLine());
			for(int line=0; line< lines; line++){
				count=0;
				input = inputStream.readLine();
				strk = new StringTokenizer(input," ");
				start = Integer.parseInt(strk.nextToken());
				end = Integer.parseInt(strk.nextToken());
				
				for(int j=start; j<=end;j++){
					for(int k=j; k<=end;k++){
						if(isRecycled(j+"",k+"")){
							count++;
						}
					}
				}
				
				if(line!=lines-1)
					writer.write("Case #" + (line+1) + ": " + count+"\r\n");
				else
					writer.write("Case #" + (line+1) + ": " + count);
				
			}
			writer.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static boolean isRecycled(String x, String y){
		int len  = x.length();
		if(x.equals(y)){
			return false;
		}
		for(int i=0; i<len;i++){
			if(x.equals(y)){
				return true;
			}
			//shift
			x = (x.substring(1) + x.charAt(0));
		}
		return false;
	}
}
