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

import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

public class RecycledNumbers {

	public static boolean isCycle(int a,int b) {
		String aa=String.valueOf(a);
		StringBuffer ba=new StringBuffer(String.valueOf(b));
		if(aa.length()!=ba.length())
			return false;
		int l=aa.length();
		for(int i=0;i<l;++i)
		{
			if(aa.equals(ba.toString()))
				return true;
			ba.append(ba.charAt(0));
			ba.deleteCharAt(0);
		}
		return false;
	}
	public static boolean sameLen(int a,int b) {
		return String.valueOf(a).length()==String.valueOf(b).length();
	}
	public static void main(String[] args) {
		int T,count,a,b,num;
		try {
			FileWriter out = new FileWriter("d://test//Cs.out");  
			//Scanner in=new Scanner(System.in);
			Scanner in=new Scanner(new File("d://test//Cs.in"));
			T=in.nextInt();
			count=0;
			while((++count)<=T)
			{
				a=in.nextInt();
				b=in.nextInt();
				num=0;
				for(int i=a;i<=b;++i)
					for(int j=i+1;sameLen(i, j)&&j<=b;++j)
						if(isCycle(i, j))
							++num;
				
				out.write("Case #"+count+": "+num+"\r\n"); 
			}
			
			in.close(); 
			out.close();  
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}

}
