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.IOException;
import java.io.PrintWriter;

public class RecycledNumbers {
	
	public static void main(String[] args) {
		RecycledNumbers bt = new RecycledNumbers();
		bt.solveProblem();
		
	}
	public void solveProblem() {
		String[] testCases = ReadFile.readFile("C:/Users/Pablo/Documents/codejam/RN.in");
		int i = 0;
		StringBuilder res = new StringBuilder();
		for (String testCase : testCases) {
			if(!res.toString().equals("")) {
				res.append("\n");
			}
			res.append(processTestCase(testCase, ++i));
		}
		System.out.print(res.toString());
		try {
			PrintWriter pw = new PrintWriter("C:/Users/Pablo/Documents/codejam/RN.out");
			pw.print(res.toString());
			pw.close();
		} catch (IOException e) {
			System.out.println("Error writing file!!");
		}
	}
	
	private String processTestCase(String testCase, int caseNum) {
		Long a = Long.valueOf(testCase.split(" ")[0]);
		Long b = Long.valueOf(testCase.split(" ")[1]);
		Long lim = Double.valueOf(Math.floor((a + b)/ 2d)).longValue() + 1L;
		
		int cantNumbers = 0;
		
		if(a.compareTo(10L) >= 0) {
			cantNumbers = verifNumbers(b, a, 0);
		} 
		
		return "Case #" + caseNum + ": " + cantNumbers;
	}
	
	private int verifNumbers(Long b, Long num, int cantNumbers) {

		for(; num.compareTo(b) < 0; num = num + 1L) {
			String n = num.toString();
			n = n.replaceAll(String.valueOf(n.charAt(0)), "");

			if(!n.equals("")) {
				for(int i = 1; i <= num.toString().length(); i++) {
					String s = num.toString().substring(num.toString().length() - i, num.toString().length()) + 
					num.toString().substring(0, num.toString().length() - i);

					Long tmp = Long.valueOf(s);

					if(tmp.compareTo(b) <= 0 && tmp.compareTo(num) > 0) {
						cantNumbers ++;
					}
				}
			}			
		}
		return cantNumbers;
	}	
}
