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);
		}
	}
}
package codejam2012Qualification;

import java.io.*;
import java.util.*;

public class C {
	public static void main(String[] args) throws IOException {
		boolean large = false;
		large = true;
		String dir = "C://Users//Aayush//Desktop//";
		String InputFile = dir + (large ? "C-large.in" : "C-small-attempt0.in");
		String OutputFile = dir + (large ? "C-large.out" : "C-small.out");
		File file = new File(InputFile);
		Scanner st = new Scanner(file);
		FileWriter fw = new FileWriter(OutputFile);
		int T = st.nextInt();
		for(int cases = 1; cases <= T; cases++){
			int A = st.nextInt();
			int B = st.nextInt();
			int digits = 0;
			int A1 = A;
			while(A1 > 0){
				A1 /= 10;
				digits++;
			}
			//Efficient solution
			int count = 0;
			HashSet<Integer> h = new HashSet<Integer>();
			for(int n = A; n < B; n++){
				for(int i = 1; i < digits; i++){
					int d = (int) Math.pow(10, i);
					int d1 = (int) Math.pow(10, digits - i);
					int r = n % d;
					int m = d1 * r + n / d;
					if(m > n && m <= B){ 
						h.add(m);
					}
				}
				count+= h.size();
				h.clear();
			}
			System.out.print("Case #" + cases + ": " + count);
			fw.write("Case #" + cases + ": " + count);

			System.out.println();
			fw.write("\n");
		}
		fw.flush();
		fw.close();

	}
}
