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

import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;

public class TestCaseSolver implements Runnable {

    private ArrayList<TestCase> testCases;
    private int ref;

    public TestCaseSolver(ArrayList<TestCase> tcs, int ref) {
        testCases = tcs;
        this.ref = ref;
    }

    public TestCaseSolver(TestCase tc, int ref) {
        ArrayList<TestCase> tcs = new ArrayList<>();
        tcs.add(tc);
        testCases = tcs;
        this.ref = ref;
    }

    public int getRef() {
        return ref;
    }

    @Override
    public void run() {
        for (TestCase tc : testCases) {
            long startTime = System.nanoTime();
            solve(tc);
            long duration = System.nanoTime() - startTime;
            double secs = (double)duration / (1000000000d);
            tc.setTime(secs);
            System.out.println("Thread " + ref + " solved testcase " + tc.getRef() + " in " + String.format("%.2f", secs) + " secs.");
        }
    }

    private void solve(TestCase tc) {
        ArrayList<Integer> a_b = tc.getIntegerList("a_b");
        int A = a_b.get(0);
        int B = a_b.get(1);
        int count = 0;
        for (int i = A; i <= B; i++) {
            ArrayList<Integer> digs = Utils.splitToDigits(i);
            int l = digs.size();
            Set<Integer> solns = new HashSet<>();
            for (int r = 1; r < l; r++) {
                int rec = recombine(digs, r);
                if (valid(digs, r) && rec <= B) {
                    //System.out.println(digs + " " + r);
                    solns.add(new Integer(rec));
                }
            }
            count += solns.size();
        }
        
        
        tc.setSolution(new Integer(count));
    }
    
    private boolean valid(ArrayList<Integer> digits, int startRotation) {
        if (digits.get(startRotation).intValue() == 0) {return false;}
        int len = digits.size();
        for (int origLoc = 0; origLoc < len; origLoc++) {
            int rotLoc = (origLoc + startRotation) % len;
            int origDig = digits.get(origLoc);
            int rotDig = digits.get(rotLoc);
            if (rotDig > origDig) {return true;}
            if (rotDig < origDig) {return false;}
        }
        return false;
        
    }
    
    private int recombine(ArrayList<Integer> digs, int start) {
        int tot = 0;
        for (int i = 0; i < digs.size(); i++) {
            int loc = (i + start) % digs.size();
            tot += digs.get(loc).intValue() * (int)(Math.pow(10d, (double)(digs.size() - i - 1)));
        }
        return tot;
    }
}
