import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

class RecycledNumbers{
	
	
	
	int getcount(int small,int large){
		int count = 0;
		for(int currnum = small; currnum < large; currnum++){
			int permut = currnum;
			int no_of_digits = 0;
			while(permut > 0){
				permut = permut/10;
				no_of_digits++;
			}
			int lastDigit = currnum % 10;
			permut = currnum / 10;
			permut = permut + lastDigit * (int)Math.pow(10, no_of_digits-1);
			while(permut != currnum){
				
				if(permut >= currnum && permut <= large){
					
					count++;
				}
				lastDigit = permut % 10;
				permut = permut / 10;
				permut = permut + lastDigit * (int)Math.pow(10, no_of_digits-1);
				
			}
			
		}
		
		return count;
	}
	
	public static void main(String args[]){
		RecycledNumbers d = new RecycledNumbers();
		
		int no_of_cases = 0;
		String input = "";
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try{
			no_of_cases = Integer.parseInt(br.readLine());
		}
		catch(Exception e){
			System.out.println("First line is not a number");
		}
		
		for(int i=1; i <= no_of_cases;i++){
			try{
				input = br.readLine();
			}
			catch(Exception e){
				System.out.println("Number of test cases different from the number mentioned in first line");
			}
			
			System.out.print("Case #" +i+": ");
			
			StringTokenizer t =  new StringTokenizer(input);
			int small=0,large=0;
			if(t.hasMoreTokens()){
				small = Integer.parseInt(t.nextToken());
			}
			if(t.hasMoreTokens()){
				large = Integer.parseInt(t.nextToken());
			}
			System.out.println(d.getcount(small,large));
			
		}
		
		
		

	}
	
	
}
package year2012.qualification.c;

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class RecycledNumbers {
	public static void main(String[] args) throws Exception{
		//GCJå±é
		String filename = "src/year2012/qualification/c/C-small-attempt0";
		PrintWriter out = new PrintWriter(new File(filename + ".out"));
		Scanner scan = new Scanner(new File(filename + ".in"));
		
		final int T = scan.nextInt();
		for(int i=0;i<T;i++){
			int ans=0;
			int A = scan.nextInt();
			int B = scan.nextInt();
			
			for(int n=A;n<B;n++){
				int m_memo = -1;
				
				char[] na = String.valueOf(n).toCharArray();
				//ä¾ãã°nã123ã®ã¨ãã231,312ã[A,B)ã®ä¸­ã«ãããèª¿ã¹ã
				//ãã®ä¾ã ã¨k=2åã«ã¼ã
				for(int k=1;k<na.length;k++){
					char[] ma = rotate(na, k);
					int m = Integer.parseInt(String.valueOf(ma));
					if(A <=n
						&& n < m
						&& m <= B
						&& m != m_memo){
						
						m_memo = m;
						ans++;
					}
				}
				
			}
			//åºå
			out.printf("Case #%d: %s\n", i+1, ans);
		}
		out.flush();
	}
	
	
	/**
	 * éåãnåå·¦ã«ããããæ°ããéåãè¿ãã¾ãã
	 * @param in
	 * @param n
	 * @return
	 */
	private static char[] rotate(final char[] in, int n){
		if(n>=in.length){
			throw new IllegalArgumentException("n= " + n + ", in.length = "
					+ in.length + "ã¨ãªã£ã¦ãã¾ããn<in.lengthã«ãã¦ãã ããã");
		}
		char[] out = new char[in.length];
		
		//1,2,3,4ã2åå·¦ã«ãããå ´åãã¾ãå·¦å´ã®2åãéé¿ãã
		char[] tmp = new char[n];
		for(int i=0;i<n;i++){
			tmp[i] = in[i];
		}
		//æ¬¡ã«in[n]ããin[in.length]ã¾ã§ãout[0]ããout[in.length - 2]ã¾ã§ã«ã³ãã¼
		for(int i=0;i<in.length-n;i++){
			out[i] = in[i+n];
		}
		//æå¾ã«out[length-n,length]ã«tmp[0,n]ããå¤ããã©ã
		for(int i=in.length-n;i<in.length;i++){
			out[i] = tmp[i-in.length+n];
		}
		return out;
	}
}
