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 com.archu.contest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashSet;

public class RecycledNum
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		File inputFile = null;
		File outputFile = null;
		BufferedReader br = null;
		BufferedWriter bw = null;
		String str = null;
		int nTestCase = 0;

		try
		{
			inputFile = new File("C-large.in");
			outputFile = new File("C-large.out");
			br = new BufferedReader(new FileReader(inputFile));
			bw = new BufferedWriter(new FileWriter(outputFile));

			str = br.readLine();
			nTestCase = Integer.parseInt(str);

			for (int k = 0; k < nTestCase; k++)
			{
				if ((str = br.readLine()) != null)
				{
					String[] range = str.split(" ");
					int start = Integer.parseInt(range[0]);
					int end = Integer.parseInt(range[1]);
					int len = range[0].length();

					HashSet<String> setRecycled = new HashSet<String>();

					if (len > 1)
					{
						for (int i = start; i <= end; i++)
						{
							String numStr = String.valueOf(i);
							char[] numChar = numStr.toCharArray();

							HashSet set = new HashSet();
							for (char ch : numChar)
							{
								set.add(ch);
							}
							if (set.size() == 1)
							{
								continue;
							}
							int origNum = Integer.parseInt(numStr);
							for (int j = 0; j < len - 1; j++)
							{
								// System.out.println("numStr :: " + numStr);
								numStr = rotateNumber(numStr, len);
								// System.out.println("after rotating numStr :: "
								// + numStr);
								int num = Integer.parseInt(numStr);
								if (num != origNum && num >= start && num <= end)
								{
									// System.out.println("Pair :: (" + origNum
									// + " , " +
									// numStr + ")");
									// System.out.println("recycled no found... adding "
									// +
									// (origNum < num ? "" + origNum + num : ""
									// + num +
									// origNum) + " to set");
									setRecycled.add(origNum < num ? "" + origNum + "," + num : "" + num + "," + origNum);
									// System.out.println("size :: " +
									// setRecycled.size());
								}
							}
						}
					}
					System.out.println(setRecycled.size());
					bw.write("Case #" + (k + 1) + ": " + setRecycled.size() + "\n");

					// TreeSet<String> set = new TreeSet<String>(setRecycled);
					// System.out.println(set);
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		try
		{
			if (br != null)
			{
				br.close();
			}
			if (bw != null)
			{
				bw.flush();
				bw.close();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

	}

	public static String rotateNumber(String num, int len)
	{
		return num.charAt(len - 1) + num.substring(0, len - 1);
	}
}
