
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;

public class Recycle
{
	public static void main(String[] args)
	{
/*		Set<Integer> perms = getPermutations(123456);
		
		for(Integer i: perms)
			System.out.println(i);*/
	
		try
		{
			Scanner s = new Scanner(new File("recycle.in"));
			BufferedWriter w = new BufferedWriter(new FileWriter(new File("recycle.out")));
			int numCases = s.nextInt();
			s.nextLine();
			
			for(int n = 1;n <= numCases; n++)
			{
				int A = s.nextInt();
				int B = s.nextInt();
				int count = 0;
				for(int k = A; k <= B; k++)
				{
					count += getNumPairs(k, B);
				}
				
				w.write("Case #" + n + ": " + count + "\n");
			}
			
			w.flush();
			w.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public static int getNumPairs(int n, int B)
	{
		Set<Integer> possibles = getPermutations(n);
		int count = 0;
		for(Integer i : possibles)
			if(i > n && i <= B)
				count++;
		return count;
	}
	
	public static Set<Integer> getPermutations(int n)
	{
		Set<Integer> perms = new TreeSet<Integer>();
		char[] digits = String.valueOf(n).toCharArray();
		
		for(int firstPos = 1; firstPos < digits.length; firstPos++)
		{
			String toBuild = "";
			for(int k = 0; k < digits.length; k++)
				toBuild += digits[(firstPos + k) % digits.length];
			perms.add(Integer.parseInt(toBuild));
		}
		return perms;
	}
	

}
package info.m3o.gcj2012.recyclednumber;


public class MainRecycledNumber {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// String infilepath = "C:\\eclipse\\workspace\\gcjRecycledNumber\\data\\inputTest.txt";
		
		// String infilepath = "C:\\eclipse\\git_home\\git\\gcjRecycledNumber1\\gcjRecycledNumber\\data\\inputLarge.txt";
		String infilepath = "C:\\eclipse\\git_home\\git\\gcjRecycledNumber1\\gcjRecycledNumber\\data\\inputSmall.txt";

		
		//		String infilepath = "C:\\eclipse\\workspace\\gcjRecycledNumber\\data\\inputSmall.txt";
		//		String infilepath = "C:\\eclipse\\workspace\\gcjRecycledNumber\\data\\inputLarge.txt";

		InputFileLoader inputFileLoader = new InputFileLoader(infilepath);


		inputFileLoader.readInputFile();


		int inT = Integer.valueOf(inputFileLoader.getRawInputLines().get(0));
		// Remove the fist line, which is no logner needed.
		inputFileLoader.getRawInputLines().remove(0);


		int inA, inB;
		int caseCount = 0;
		// System.out.println("T=" + inT);

		ResultUtil resultUtil = new ResultUtil();

		for (String line : inputFileLoader.getRawInputLines()){
			String[] s = line.split("\\s");

			inA = Integer.valueOf(s[0]);
			inB = Integer.valueOf(s[1]);
			// System.out.println("A=" + inA + "B=" + inB );
			caseCount++;

			/*
			 *  Start the actual Job
			 */
			resultUtil.clearSets();
			int n, numRecycledNumber;
			RecycledNumber recycledNumber;

			
			for (n = inA; n <= inB; n++) {
				recycledNumber = new RecycledNumber(n, inA, inB);
				recycledNumber.addRecycledNumbers(resultUtil.recycledNumberSet,
						resultUtil.testedNumberSet);
				for (String str : RecycledNumber.getCycledNumber(recycledNumber
						.getNumStr())) {
					recycledNumber.addRecycledNumbers(resultUtil.recycledNumberSet,
							resultUtil.testedNumberSet, str);
					
				}

				// recycledNumber.addRecycledNumbers(resultUtil.recycledNumberSet,
				// resultUtil.testedNumberSet);
			}			numRecycledNumber = resultUtil.recycledNumberSet.size();
			

			
			
			System.out.print("Case #"+ caseCount+": ");
			System.out.println(numRecycledNumber);
			/*
			for (MyNumberStringPair mnsp: resultUtil.recycledNumberSet){
				System.out.print(mnsp.getS1() + " " + mnsp.getS2() + " " );
			}
			System.out.println("END");
			*/
		}

		/*
		System.out.println("caseCount="+caseCount);
		*/
		assert (inT == caseCount) : "CaseCount mismatch";

	}

}
