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

import java.util.List;
import java.util.Set;

import util.ListFactory;
import util.SetFactory;
import file.Input;
import file.Output;

public class Recycled {

	public void start() {
		List<String> inputLines = Input.getLines();
		if (inputLines.size() > 0) {
			List<String> outputLines = ListFactory.newArrayListInstance();
			int t = Integer.parseInt(inputLines.get(0));
			for (int i = 1; i <= t; i++) {
				String[] parts = inputLines.get(i).split(" ");
				int a = Integer.parseInt(parts[0]);
				int b = Integer.parseInt(parts[1]);
				outputLines.add("Case #" + i + ": " + getNumber(a, b));
			}
			Output.writeLines(outputLines);
		}
	}

	private int getNumber(int a, int b) {
		Set<Pair> set = SetFactory.newHashSetInstance();
		if (a <= b) {
			for (int n = a; n <= b; n++) {
				int length = getLength(n);
				for (int i = 0; i < length - 1; i++) {
					int m = moveRight(n, i + 1);
					if (n < m && m <= b) {
						set.add(new Pair(n, m));
					}
				}
			}
		}
		return set.size();
	}

	private int getLength(int number) {
		int length = 0;
		if (number >= 0 && number <= 9) {
			length = 1;
		} else if (number >= 10 && number <= 99) {
			length = 2;
		} else if (number >= 100 && number <= 999) {
			length = 3;
		} else if (number >= 1000 && number <= 9999) {
			length = 4;
		} else if (number >= 10000 && number <= 99999) {
			length = 5;
		}
		return length;
	}

	private int moveRight(int n, int times) {
		String number = String.valueOf(n);
		for (int i = 0; i < times; i++) {
			int length = number.length();
			number = number.substring(length - 1, length)
					+ number.substring(0, length - 1);
		}
		return Integer.parseInt(number);
	}

	public static void main(String[] args) {
		new Recycled().start();
	}

}
