import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Recycled {
	public static void main(String[] args) throws Exception{
		String inputFile = "C-small-attempt0.in";
		
		BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));
		BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output")));
		long num_cases = Long.parseLong(input.readLine());
		int current_case = 0;
		
		while (current_case < num_cases){
			current_case++;
			
			String[] fields = input.readLine().split(" ");
			int A = Integer.parseInt(fields[0]);
			int B = Integer.parseInt(fields[1]);
			int total = 0;
			
			for (int n = A; n < B; n++){
				for (int m = n+1; m <= B; m++){
					if (isRecycled(n,m)) total++;
				}
			}
			
			System.out.println("Case #" + current_case + ": " + total);
			output.write("Case #" + current_case + ": " + total + "\n");
		}
		output.close();
	}

	private static boolean isRecycled(int n, int m) {
		String sn = ""+n;
		String sm = ""+m;
		if (sn.length() != sm.length()) return false;
		int totaln = 0, totalm = 0;
		for (int i = 0; i < sn.length(); i++){
			totaln += sn.charAt(i);
			totalm += sm.charAt(i);
		}
		if (totaln != totalm) return false;
		for (int i = 0; i < sn.length()-1; i++){
			sm = sm.charAt(sm.length() - 1) + sm.substring(0, sm.length() - 1);
			if (sm.equals(sn)) return true;
		}
		return false;
	}
}



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;



public class RecycledNumbers{
	public static void main(String[] args) throws IOException{
		String testCases;
		BufferedReader brTestCases = new BufferedReader(new InputStreamReader(System.in));
		File inFile = new File("C:/Users/AbhilashR/Downloads/C-small-attempt0.in" ); 
		Scanner sc = new Scanner(inFile);
		//testCases = (String)brTestCases.readLine();
		testCases = sc.nextLine();
		Integer noOfTestCases = null;
		try{
			noOfTestCases = Integer.parseInt(testCases);
		}catch (NumberFormatException e){
			System.out.println("The entered value is not a Number");
		}
		ArrayList<String> inputData = new ArrayList<String>();
		for(int i =0;i<noOfTestCases;i++){
			brTestCases = new BufferedReader(new InputStreamReader(System.in));
			//testCases = (String)brTestCases.readLine();
			testCases = sc.nextLine();
			inputData.add(testCases);
		}
		
		ArrayList<Object[]> inputDataObj = new ArrayList<Object[]>();
		for(int i =0;i<inputData.size();i++){
			inputDataObj.add(inputData.get(i).split(" "));
		}
		Writer output = null;
		File outFile = new File("C:/Users/AbhilashR/Downloads/outPut.txt" );
		output = new BufferedWriter(new FileWriter(outFile));
		String text = "";
		for(int i =0;i < inputDataObj.size();i++){
			Object[] dataObj = inputDataObj.get(i);
			int integerA = Integer.parseInt((String)dataObj[0]);
			int integerB = Integer.parseInt((String)dataObj[1]);
			int recycledNumbers = getRecycledNumbers(integerA, integerB);
			text = "Case #"+(i+1)+": "+recycledNumbers+"\r\n";
			output.write(text);
			output.flush();
			
		}
		//output.write(text);
		output.close();
	}
	public static Integer getRecycledNumbers(Integer a,Integer b){
		Integer noOfRecycledNumbers = 0;
		Integer sampleN =a;
		
		Integer numberOfDigits = 0;
		numberOfDigits = Integer.toString(a).length();
		for(sampleN =a; sampleN < b ;sampleN++){
			Integer sampleM = sampleN;
			Set<Integer> tempSet = new HashSet<Integer>();
			for(int j =0; j< numberOfDigits; j++){
				int currentSize = Integer.toString(sampleM).length();
				if(currentSize==numberOfDigits){
					sampleM = Integer.parseInt(Integer.toString(sampleM%10)+Integer.toString(sampleM/10));
				}else{
					String formingSampleM = Integer.toString(sampleM%10);
					for(int k =0;k<numberOfDigits-currentSize;k++){
						formingSampleM = formingSampleM+"0";
					}
					formingSampleM = formingSampleM+Integer.toString(sampleM/10);
					sampleM = Integer.parseInt(formingSampleM);
				}
				if(sampleN<sampleM && sampleM<=b){
					tempSet.add(sampleM);
				}
			}	
			noOfRecycledNumbers += tempSet.size();
		}
		if(noOfRecycledNumbers>0){
			return noOfRecycledNumbers;
		}
		return 0;
	}
	
}