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 org.sooo;

import java.io.File;
import java.io.OutputStreamWriter;
import java.util.List;

import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import com.google.common.io.OutputSupplier;

public class RecycledNumbers {

	public static boolean isValidPair(long n, long m) {
		String nStr = Long.toString(n);
		String mStr = Long.toString(m);
		for (int i = 0; i < nStr.length(); i++) {
			String shifted = nStr.substring(i) + nStr.substring(0, i);
			if (shifted.charAt(0) != '0' && shifted.equals(mStr))
				return true;
		}
		return false;
	}

	public static void main(String[] args) throws Exception {
		//String path = "src/main/resources/C-sample.in";
		String path = "src/main/resources/C-small-attempt0.in";
		List<String> lines = Files.readLines(new File(path), Charsets.UTF_8);

		OutputSupplier<OutputStreamWriter> outputSupplier = Files
				.newWriterSupplier(new File(path + ".out"), Charsets.UTF_8);
		StringBuilder results = new StringBuilder();
		for (int i = 1; i < lines.size(); i++) {
			List<String> tokens = Lists.newArrayList(Splitter.on(' ').split(
					lines.get(i)));
			long lowerBound = Long.parseLong(tokens.get(0));
			long upperBound = Long.parseLong(tokens.get(1));
			long count = 0;

			if (lowerBound >= 10) {
				for (long n = lowerBound; n < upperBound; n++) {
					for (long m = n + 1; m <= upperBound; m++) {
						if (RecycledNumbers.isValidPair(n, m))
							count++;
					}
				}
			}

			String result = String.format("Case #%d: %d\n", i, count);
			System.out.print(result);
			results.append(result);
		}
		CharStreams.write(results, outputSupplier);
	}
}
