
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 com.ignaciobona.codejam.practice.problema;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.HashMap;

public class Solver {
	
	public static String PROBLEM_NUMBER="C";

	public static String PATH_TO_INPUT_FILE_SMALL="inputs/"+PROBLEM_NUMBER+"-small-attempt0.in";
	public static String PATH_TO_OUPUT_FILE_SMALL="outputs/"+PROBLEM_NUMBER+"-small-attempt0.out";
	

	public static String PATH_TO_INPUT_FILE_LARGE="inputs/"+PROBLEM_NUMBER+"-large.in";
	public static String PATH_TO_OUPUT_FILE_LARGE="outputs/"+PROBLEM_NUMBER+"-large.out";

	public static boolean SMALL=true;

	
	public static class Problem{
		public int nCase;
		public int a;
		public int b;
		public StringBuffer outputString;
		
		
		public void solve(){
			HashMap<Integer,Integer>map=new HashMap<Integer,Integer>();
			outputString=new StringBuffer("Case #"+nCase+": ");
			int found=0;
			for(int n=a;n<=b;n++){
				int nDigits=(n+"").length();
				int m=n;
				for(int j=1;j<nDigits;j++){
					int number=m%10;
					int rest=m/10;
					m=(int) (number*Math.pow(10, nDigits-1)+rest);
					
					if(a<=n&&n<m&&m<=b&&(map.get(n)==null||map.get(n)!=m)){
						//System.out.println(n+" "+m);
						map.put(n, m);
						found++;
					}
				}
				
			}
			outputString.append(found);
			
		}
	}
	
	//Boring IO handling
	public static void main(String[]args){
		try{
			String output=PATH_TO_OUPUT_FILE_LARGE;
			if(SMALL){
				output=PATH_TO_OUPUT_FILE_SMALL;
			}
			File file=new File(output);
			file.createNewFile();
			BufferedWriter bw=new BufferedWriter(new FileWriter(file));
			
		
			String input=PATH_TO_INPUT_FILE_LARGE;
			if(SMALL){
				input=PATH_TO_INPUT_FILE_SMALL;
			}
			FileInputStream fstream = new FileInputStream(input);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			

			String strLine;
			//Read File Line By Line
			int nCases=0;
			int nCase=1;	
			Problem problem=new Problem();
			for (int i=0;(strLine = br.readLine()) != null;i++)   {
				// Print the content on the console
				//System.out.println (strLine);
				if(i== 0){
						nCases=Integer.parseInt(strLine);
				}else{
					int nParam=(i-1)%1;
					switch(nParam){
						case 0:
							problem.nCase=nCase;
							
							
							String[]tokens=strLine.split(" ");
							problem.a=Integer.parseInt(tokens[0]);
							problem.b=Integer.parseInt(tokens[1]);
							
							
							problem.solve();
							System.out.println(problem.outputString);
							bw.append(problem.outputString);
							bw.newLine();
							problem=new Problem();
							nCase++;
							break;
						default:
							throw new Exception("Error Parsing");
					}
				}
			}
			//Close the input stream
			in.close();
			bw.close();
		}catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}



}


