
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 recycledNumbers;

import java.util.ArrayList;

public class Algorithms {

	public static OutputData getOutput(InputData inputdata) {
		// TODO Auto-generated method stub
		int [] Results = new int[inputdata.getTest_Number()];
		
		for(int i=0;i<Results.length;i++){
			Results[i] = getSingleResult(inputdata.getA()[i], inputdata.getB()[i]);
		}
		
		OutputData outputdata = new OutputData(Results);
		return outputdata;
	}

	private static int getSingleResult(int a, int b) {
		// TODO Auto-generated method stub
		int result = 0;
		for(int i=a;i<b+1;i++){
			ArrayList<Integer> number = new ArrayList<Integer>();
			ArrayList<Integer> numbers = new ArrayList<Integer>();
			int temp = i; 
			while(temp/10>0){
				number.add(temp%10);
				temp = temp/10;
			}
			number.add(temp%10);
			
			for(int j=0;j<number.size();j++){
				numbers.clear();
				for(int k=0;k<number.size();k++){
					numbers.add(number.get(k));
				}
				ArrayList<Integer> part = new ArrayList<Integer>();
				for(int k=0;k<j;k++){
					int temp1 = numbers.remove(0);
					part.add(temp1);
				}
				numbers.addAll(part);
				
				int newint = 0;
				for(int k=0;k<numbers.size();k++){
					newint += numbers.get(k)*pow(10,k);
				}
				//System.out.println(i+" "+newint+" "+j);
				if(i<newint && newint<=b){
					System.out.println(i+" "+newint+" "+j);
					result++;
				}
			}
			//System.exit(0);
		}
		return result;
	}

	private static Integer pow(int i, int k) {
		// TODO Auto-generated method stub
		if(k==0){
			return 1;
		}else{
			return pow(i, k-1)*i;
		}
	}


}
