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

public class C {

    void solve() throws IOException {
        in("C-large.in"); out("C-large.out");

        long tm = System.currentTimeMillis();
        boolean[] mask = new boolean[2000000];
        int[] r = new int[10];
        int t = readInt();
        for (int cs = 1; cs <= t; ++cs) {
            int a = readInt();
            int b = readInt();
            int ans = 0;
            for (int m = a + 1; m <= b; ++m) {
                char[] d = String.valueOf(m).toCharArray();
                int c = 0;
                for (int i = 1; i < d.length; ++i) {
                    if (d[i] != '0' && d[i] <= d[0]) {
                        int n = 0;
                        for (int j = 0; j < d.length; ++j) {
                            int jj = i + j;
                            if (jj >= d.length) jj -= d.length;
                            n = n * 10 + (d[jj] - '0');
                        }
                        if (n >= a && n < m && !mask[n]) {
                            ++ans;
                            mask[n] = true;
                            r[c++] = n;
                        }
                    }
                }
                for (int i = 0; i < c; ++i) {
                    mask[r[i]] = false;
                }
            }
            println("Case #" + cs + ": " + ans);
            //System.out.println(cs);
        }
        System.out.println("time: " + (System.currentTimeMillis() - tm));
        exit();
    }

    void in(String name) throws IOException {
        if (name.equals("__std")) {
            in = new BufferedReader(new InputStreamReader(System.in));
        } else {
            in = new BufferedReader(new FileReader(name));
        }
    }

    void out(String name) throws IOException {
        if (name.equals("__std")) {
            out = new PrintWriter(System.out);
        } else {
            out = new PrintWriter(name);
        }
    }

    void exit() {
        out.close();
        System.exit(0);
    }

    int readInt() throws IOException {
        return Integer.parseInt(readToken());
    }

    long readLong() throws IOException {
        return Long.parseLong(readToken());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readToken());
    }

    String readLine() throws IOException {
        st = null;
        return in.readLine();
    }

    String readToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    boolean eof() throws IOException {
        return !in.ready();
    }

    void print(String format, Object ... args) {
        out.println(new Formatter(Locale.US).format(format, args));
    }

    void println(String format, Object ... args) {
        out.println(new Formatter(Locale.US).format(format, args));
    }

    void print(Object value) {
        out.print(value);
    }

    void println(Object value) {
        out.println(value);
    }

    void println() {
        out.println();
    }

    StringTokenizer st;

    BufferedReader in;
    PrintWriter out;

    public static void main(String[] args) throws IOException {
        new C().solve();
    }
}

package codejam2012.qualification;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;


public class RecycledNumbers {
		static String dir="/home/casatta/Scaricati/";
//	static String dir="data/";

	//	static String fname="B-large.in";
//	static String fname="C-small-attempt0.in";
	static String fname="C-large.in";
//	static String fname="RecycledNumbers";


	public void test(Scanner input, PrintWriter out, Long i) {
		int a=input.nextInt();
		int b=input.nextInt();

		int count=0;

		for(int j=a;j<=b;j++) {
			count=count+countGoodRecycled(j, b);

		}

		String result = "Case #" + (i+1) + ": " +count;
		out.println(result);
		System.out.println(result);
		System.out.println();
	}

	int countGoodRecycled(int val,int maxVal) {
		String valString=""+val;
		int length=valString.length();
		Set<Integer> results=new HashSet<Integer>();

		for(int i=length-1;i>0;i--) {
			if(valString.charAt(i)!='0') {
				String after =valString.substring(i);
				String before=valString.substring(0,i);
				int inverse=Integer.parseInt( after+before );
				if(inverse<=maxVal && val<inverse) {
//					System.out.println(val + "=" + inverse);
					results.add(inverse);
					
				}
			}

		}

		return results.size();

	}

	public void run() {


		try {
			String inputPath = dir + fname;

			Scanner input =new Scanner(new File(inputPath));
			FileWriter outFile = new FileWriter(inputPath + ".out");
			PrintWriter out = new PrintWriter(outFile);
			try {		
				Long testCases = input.nextLong();
				input.nextLine();
				long start=System.currentTimeMillis();
				for(Long i=0L ; i < testCases ; i++)
					test(input,out,i);
				System.out.println("elapsed:" + (System.currentTimeMillis()-start) );
			}
			finally {
				input.close();
				out.close();
			}
		}
		catch (IOException ex){
			ex.printStackTrace();
		}
	}

	public static void main(String[] args) {
		RecycledNumbers template = new RecycledNumbers();
		template.run();
	}


}
