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

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

/**
 * Author: Andriy Gusyev
 * Date: 14.04.12
 */
public class Task2 {

    static private int[] multiplers = {0, 0, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 10000000};

    static private int cases;

    static private int[] lowerBounds;

    static private int[] upperBounds;

    static private int[] results;

    private static void readBounds(String s, int index) {
        s = s.trim();
        String[] arr = s.split(" ");
        lowerBounds[index] = Integer.parseInt(arr[0]);
        upperBounds[index] = Integer.parseInt(arr[1]);
    }

    public static void main(String args[]) throws Exception {

        Scanner sc = new Scanner(new File("d:/codejam/2/small"), "UTF-8");
        cases = Integer.parseInt(sc.nextLine());
        lowerBounds = new int[cases];
        upperBounds = new int[cases];
        results = new int[cases];
        for (int i = 0; i < cases; i++) {
            readBounds(sc.nextLine(), i);
        }
        System.out.println(cases);
        System.out.println(Arrays.toString(lowerBounds));
        System.out.println(Arrays.toString(upperBounds));

        for (int i = 0; i < cases; i++) {
            results[i] = calcRecycled(i);
        }
        System.out.println(Arrays.toString(results));
        FileWriter fw = new FileWriter(new File("d:/codejam/2/small_out"));
        int k = 0;
        for (int i : results) {
            k++;
            fw.write(String.format("Case #%s: %s\n", String.valueOf(k), String.valueOf(i)));
        }
        fw.close();
    }

    private static int calcRecycled(int index) {
        int result = 0;
        int lower = lowerBounds[index];
        int upper = upperBounds[index];
        for (int i = lower; i <= upper; i++) {
            System.out.println(i);
            int[] possibles = getPossibleRecycled(i);
            for (int j : possibles) {
                if (i < j  && j <= upper && areRecycled(i, j)) {
                    result++;
                }
            }
        }

        return result;
    }

    private static boolean areRecycled(int i, int j) {
//        if (!Arrays.equals(toSortedChars(i), toSortedChars(j))) {
//            return false;
//        }
        int digits = String.valueOf(i).length();
        int initialI = i;
        int initialJ = j;
        for (int p = 0; p < digits; p++) {
            i = changeValue(i, digits);
            if (i == j) {
                return true;
            }
        }
        ;
        return false;
    }

    private static int[] getPossibleRecycled(int i) {
        //        if (!Arrays.equals(toSortedChars(i), toSortedChars(j))) {
        //            return false;
        //        }
        int digits = String.valueOf(i).length();
        int possibles[] = new int[digits];
        for (int p = 0; p < digits; p++) {
            int candidate = changeValue(i, digits);
            i = candidate;
            if (isInArray(candidate, possibles)) {
                possibles[p] = 0;
            } else {
                possibles[p] = candidate;
            }
        }
        return possibles;
    }
    
    private static boolean isInArray(int c, int[] array) {
        for (int i : array) {
            if (i == c) {
                return true;
            }
        }
        return false;
    }

    private static int changeValue(int i, int digits) {
        if (i < 10) {
            return i;
        } else {
            //get last digit
            int last = i % 10;
            //cut last digit
            i = i / 10;
            // add last digit to the front
            i = multiplers[digits] * last + i;
            return i;
        }
    }

    private static char[] toSortedChars(int i) {
        char[] chars = String.valueOf(i).toCharArray();
        Arrays.sort(chars);
        return chars;
    }

}