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

/**
 * Copyright 2012 Christopher Schmitz. All Rights Reserved.
 */

package com.isotopeent.codejam.lib;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Utils {

	private static final String FILE_IN_EXTENSION = ".in";
	private static final String FILE_OUT_EXTENSION = ".out";
	private static final String OUT_DATE_FORMAT = "_yyyy-MM-dd_HH-mm-ss";

	public static <T> void solve(String filePath, String fileName, InputConverter<T> inputConverter, SolverBase<T> solver) {
		File folder = new File(filePath);
		File fin = getInputFile(folder, fileName);
		File fout = getOutputFile(folder, fileName);
		if (!fout.exists()) {
			try {
				fout.createNewFile();
			} catch (IOException e) {
				System.err.println("Error creating file " + fout.getAbsolutePath());
				e.printStackTrace();
			}
		}
		InputReader<T> reader = null;
		SolutionWriter writer = null;
		try {
			reader = new InputReader<T>(fin, inputConverter);
			solver.init(reader);
			writer = new SolutionWriter(fout);
	
			int caseNum = 1;
			String solution;
			long startTime = System.currentTimeMillis();
			while ((solution = solver.solveNext()) != null) {
				writer.writeSolution(caseNum++, solution);
				writer.flush();
			}
			int time = (int)(System.currentTimeMillis() - startTime);
			System.out.println("Done in " + (time / 1000) + " seconds (" + (time / (caseNum - 1)) + "ms average)");
		} catch (Exception e) {
			System.err.println("Error solving");
			e.printStackTrace();
		}
		if (reader != null) {
			try {
				reader.close();
			} catch (IOException e) {
				// do nothing
			}
		}
		if (writer != null) {
			writer.flush();
			writer.close();
		}
	}

	public static File getInputFile(File folder, String fileName) {
		return new File(folder, fileName + FILE_IN_EXTENSION);
	}

	public static File getOutputFile(File folder, String fileName) {
		DateFormat dateFormat = new SimpleDateFormat(OUT_DATE_FORMAT);
		String date = dateFormat.format(Calendar.getInstance().getTime());
		return new File(folder, fileName + date + FILE_OUT_EXTENSION);
	}

	public static String[] convertToStringArray(String line) {
		return line.split(" ");
	}

	public static int[] convertToIntArray(String line, int count) {
		int[] items = new int[count];
		int wordStart = 0;
		for (int i = 0; i < count; i++) {
			int wordEnd = line.indexOf(' ', wordStart);
			if (wordEnd < 0) {
				wordEnd = line.length();
			}
			int param = Integer.parseInt(line.substring(wordStart, wordEnd));
			items[i] = param;
			wordStart = wordEnd + 1;
		}
		return items;
	}

	public static int convertToIntArray(String line, int[] itemsOut) {
		int count = 0;
		int wordStart = 0;
		int length = line.length();
		while (wordStart < length) {
			int wordEnd = line.indexOf(' ', wordStart);
			if (wordEnd < 0) {
				wordEnd = line.length();
			}
			int param = Integer.parseInt(line.substring(wordStart, wordEnd));
			itemsOut[count++] = param;
			wordStart = wordEnd + 1;
		}
		return count;
	}

}
