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 de.at.codejam;

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

import de.at.codejam.util.AbstractOutputFileWriter;
import de.at.codejam.util.CaseSolver;
import de.at.codejam.util.InputFileParser;
import de.at.codejam.util.StatusListener;
import de.at.codejam.util.TaskStatus;

public abstract class AbstractCodeJamProblemSolver<CASE> implements
		StatusListener {

	private final List<StatusListener> statusListenerList = new ArrayList<StatusListener>();

	private TaskStatus taskStatus = null;

	private CaseSolver<CASE> currentCaseSolver;

	public void solveInputFile(File inputFile, File outputFile) {

		taskStatus = new TaskStatus();

		final InputFileParser<CASE> inputFileParser = createInputFileParser(inputFile);
		final AbstractOutputFileWriter<CASE> outputFileWriter = createOutputFileWriter(outputFile);

		inputFileParser.initialize(taskStatus);
		log(LOGLEVEL_INFO, "Initialized: " + taskStatus);

		Thread caseSolvingProcess = new Thread(new Runnable() {

			@Override
			public void run() {

				taskStatus.setCurrentTaskStatus(TaskStatus.STATUS_RUNNING);

				int i = 0;
				try {

					while (inputFileParser.hasNextCase()) {

						final CASE currentCase = inputFileParser.getNextCase();

						i++;
						taskStatus.setNumberCurrentCase(i);

						String result = solveCase(taskStatus, currentCase);
						log(StatusListener.LOGLEVEL_INFO, result);
						outputFileWriter.appendResult(result);

						try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
						}
					}

					taskStatus.setCurrentTaskStatus(TaskStatus.STATUS_DONE);

				} catch (Exception exc) {

					exc.printStackTrace();

					log(LOGLEVEL_ERROR,
							"Exception while reading / solving cases: "
									+ exc.getMessage());
					taskStatus.setCurrentTaskStatus(TaskStatus.STATUS_ERROR);
				}
			}
		});
		caseSolvingProcess.start();

		Thread progressUpdateProcess = new Thread(new Runnable() {
			@Override
			public void run() {

				while (!taskStatus.equals(TaskStatus.STATUS_DONE)) {

					updateStatus(taskStatus);

					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
					}
				}
				updateStatus(taskStatus);
			}
		});
		progressUpdateProcess.start();
	}

	private String solveCase(TaskStatus taskStatus, CASE caseToSolve) {

		if ((null == currentCaseSolver)
				|| !useCaseSolver(currentCaseSolver, caseToSolve)) {
			currentCaseSolver = buildCaseSolver(caseToSolve);
			currentCaseSolver.addStatusListener(this);
		}

		return currentCaseSolver.solveCase(taskStatus, caseToSolve);
	}

	protected abstract boolean useCaseSolver(CaseSolver<CASE> caseSolver,
			CASE caseToSolve);

	protected abstract CaseSolver<CASE> buildCaseSolver(CASE caseToSolve);

	protected abstract InputFileParser<CASE> createInputFileParser(
			File inputFile);

	protected abstract AbstractOutputFileWriter<CASE> createOutputFileWriter(
			File outputFile);

	@Override
	public void updateStatus(TaskStatus taskStatus) {
		for (StatusListener listener : statusListenerList) {
			listener.updateStatus(taskStatus);
		}
	}

	@Override
	public void log(int logLevel, String message) {
		for (StatusListener listener : statusListenerList) {
			listener.log(logLevel, message);
		}
	}

	public void addStatusListener(StatusListener statusListener) {
		statusListenerList.add(statusListener);
	}

	public void removeStatusListener(StatusListener statusListener) {
		statusListenerList.remove(statusListener);
	}
}
