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 com.renoux.gael.codejam.fwk;

import java.io.File;

import com.renoux.gael.codejam.utils.FluxUtils;
import com.renoux.gael.codejam.utils.Input;
import com.renoux.gael.codejam.utils.Output;
import com.renoux.gael.codejam.utils.Timer;

public abstract class Solver {

	public void run() {
		Timer.start();
		if (!disableSample())
			runOnce(getSampleInput(), getSampleOutput());
		System.out.println(Timer.check());

		Timer.start();
		if (!disableSmall())
			runOnce(getSmallInput(), getSmallOutput());
		System.out.println(Timer.check());

		Timer.start();
		if (!disableLarge())
			runOnce(getLargeInput(), getLargeOutput());
		System.out.println(Timer.check());

	}

	public void runOnce(String pathIn, String pathOut) {
		Input in = null;
		Output out = null;
		try {

			in = Input.open(getFile(pathIn));
			out = Output.open(getFile(pathOut));

			int countCases = Integer.parseInt(in.readLine());
			for (int k = 1; k <= countCases; k++) {
				out.write("Case #", k, ": ");
				solveCase(in, out);
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			FluxUtils.closeCloseables(in, out);
		}
	}

	private File getFile(String path) {
		return new File(path);
	}

	protected abstract void solveCase(Input in, Output out);

	private String getFullPath(boolean input, String type) {
		if (input)
			return "D:\\Downloads\\CodeJam\\" + getProblemName() + "\\"
					+ getProblemName() + "_input_" + type + ".txt";
		else
			return "D:\\Downloads\\CodeJam\\" + getProblemName() + "\\"
					+ getProblemName() + "_output_" + type + ".txt";
	}

	protected String getSampleInput() {
		return getFullPath(true, "sample");
	}

	protected String getSmallInput() {
		return getFullPath(true, "small");
	}

	protected String getLargeInput() {
		return getFullPath(true, "large");
	}

	protected String getSampleOutput() {
		return getFullPath(false, "sample");
	}

	protected String getSmallOutput() {
		return getFullPath(false, "small");
	}

	protected String getLargeOutput() {
		return getFullPath(false, "large");
	}

	protected boolean disableSample() {
		return disable()[0];
	}

	protected boolean disableSmall() {
		return disable()[1];
	}

	protected boolean disableLarge() {
		return disable()[2];
	}

	protected boolean[] disable() {
		return new boolean[] { false, false, false };
	}

	protected abstract String getProblemName();
}
