package codejam;

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

public class RecycledNumbers {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader("in.txt"));
		PrintWriter out = new PrintWriter(new File("out.txt"));
		
		int T = Integer.parseInt(in.readLine());
		for (int t = 1; t <= T; ++t) {
			String[] parts = in.readLine().split("[ ]+");
			int A = Integer.parseInt(parts[0]);
			int B = Integer.parseInt(parts[1]);
			
			int cnt = 0;
			for (int i = A; i < B; ++i) {
				String str = String.valueOf(i);
				int n = str.length();
				String res = "";
				Set<Integer> seen = new HashSet<Integer>();
				for (int j = n - 1; j > 0; --j) {
					res = str.substring(j) + str.substring(0, j);
					int k = Integer.parseInt(res);
					if (k > i && k <= B) {
						//System.out.println("(" + i + ", " + k + ")");
						if (!seen.contains(k)) {
							++cnt;
							seen.add(k);
						}
					}
				}
			}

			out.println("Case #" + t + ": " + cnt);
		}
		
		in.close();
		out.close();
		System.exit(0);
	}
}

package codejam2012qual;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintStream;
import java.io.PrintWriter;

import util.InputReader;

public class RecycledNumbers implements Runnable {

	private InputReader in;
	private PrintWriter out;
	private static String delimiter = " ";
	
	public static void main(String[] args) {
		new Thread(new RecycledNumbers()).start();
	}
	
	public RecycledNumbers() {
		try {			
			BufferedReader buffer = new BufferedReader(new FileReader("d:\\C-small-attempt0.in")); 
			in = new InputReader(buffer);
			
			System.setOut(new PrintStream(new FileOutputStream("d:\\output.txt")));
			out = new PrintWriter(System.out);
			
		} catch (FileNotFoundException e) {
			throw new RuntimeException();
		}	
	}
	
	@Override
	public void run() {
		
		int numTests = in.readInt();		
		
        for (int testNumber = 0; testNumber < numTests; testNumber++) {
        	
			out.print("Case #" + (testNumber + 1) + ": ");
        
			String line = in.readString();
			String[] boundary = line.split(delimiter);
			int min = Integer.parseInt(boundary[0]);
			int max = Integer.parseInt(boundary[1]);
			//out.println("min = "+min +"\tmax = "+max);
			
			int counter = 0;
			
			for(int i = min; min < max; max--) {
				i = min;
				
				for(; i< max; i++) {
					//out.println("i = "+i +"\tmax = "+max);
					if(testRecycle(i, max) == true)
						counter++;
				}
			}			

			out.println(counter);
		}
		out.close();
	}

	private boolean testRecycle(int a, int b) {

		String strA = a+"";
		String strB = b+"";		
		String newStrB = strB + strB;
		
		if(newStrB.indexOf(strA) > -1)
			return true;
		
		return false;
	}	

}
