
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 algorithm.adhoc;

import java.io.*;
import java.util.*;
import java.math.*;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 4/15/12
 * Time: 1:00 AM
 * To change this template use File | Settings | File Templates.
 */
public class GoogleCodeJam {
    public static void main(String[] arg) throws IOException {
        Scanner scanner = new Scanner(new FileReader("G:\\C-small-attempt1.in"));
        PrintWriter pw = new PrintWriter("output.txt");
        int t, tcase = 1;
        int a, b, start, end, temp;
        int count = 0;
        t = scanner.nextInt();
        String tempStr, tempStr2;
        char[] chars;
        char tmp, temp1;
        while (t > 0) {
            count = 0;
            a = scanner.nextInt();
            b = scanner.nextInt();
            start = a;
            end = b;
            for (int i = start; i <= end; ++i) {
                tempStr = Integer.toString(i);
                if (tempStr.length() != 1) {
                    if (tempStr.length() == 2) {
                        chars = tempStr.toCharArray();
                        tmp = chars[0];
                        chars[0] = chars[1];
                        chars[1] = tmp;
                        tempStr2 = new String(chars);
                        temp = Integer.parseInt(tempStr2);
                        if (temp > i && temp <= b) ++count;
                    } else if (tempStr.length() == 3) {
                        chars = tempStr.toCharArray();
                        tmp = chars[0];
                        chars[0] = chars[2];
                        chars[2] = tmp;
                        tmp = chars[1];
                        chars[1] = chars[2];
                        chars[2] = tmp;
                        tempStr2 = new String(chars);
                        temp = Integer.parseInt(tempStr2);
                        if (temp > i && temp <= b) ++count;
                        chars = tempStr.toCharArray();
                        tmp = chars[0];
                        chars[0] = chars[1];
                        chars[1] = tmp;
                        tmp = chars[1];
                        chars[1] = chars[2];
                        chars[2] = tmp;
                        tempStr2 = new String(chars);
                        temp = Integer.parseInt(tempStr2);
                        if (temp > i && temp <= b) ++count;
                    }
                }
            }

            //System.out.println(count);
            pw.println("Case #" + tcase + ": " + count);
            --t;
            ++tcase;
        }
        pw.close();
        scanner.close();
    }
}
