package fixjava;

public class StringUtils {

	/** Repeat the given string the requested number of times. */
	public static String repeat(String str, int numTimes) {
		StringBuilder buf = new StringBuilder(Math.max(0, str.length() * numTimes));
		for (int i = 0; i < numTimes; i++)
			buf.append(str);
		return buf.toString();
	}

	/**
	 * Pad the left hand side of a field with the given character. Always adds at least one pad character. If the length of str is
	 * greater than numPlaces-1, then the output string will be longer than numPlaces.
	 */
	public static String padLeft(String str, char padChar, int numPlaces) {
		int bufSize = Math.max(numPlaces, str.length() + 1);
		StringBuilder buf = new StringBuilder(Math.max(numPlaces, str.length() + 1));
		buf.append(padChar);
		for (int i = 1, mi = bufSize - str.length(); i < mi; i++)
			buf.append(padChar);
		buf.append(str);
		return buf.toString();
	}

	/**
	 * Pad the right hand side of a field with the given character. Always adds at least one pad character. If the length of str is
	 * greater than numPlaces-1, then the output string will be longer than numPlaces.
	 */
	public static String padRight(String str, char padChar, int numPlaces) {
		int bufSize = Math.max(numPlaces, str.length() + 1);
		StringBuilder buf = new StringBuilder(Math.max(numPlaces, str.length() + 1));
		buf.append(str);
		buf.append(padChar);
		while (buf.length() < bufSize)
			buf.append(padChar);
		return buf.toString();
	}

	/** Intern all strings in an array, skipping null elements. */
	public static String[] intern(String[] arr) {
		for (int i = 0; i < arr.length; i++)
			arr[i] = arr[i].intern();
		return arr;
	}

	/** Intern a string, ignoring null */
	public static String intern(String str) {
		return str == null ? null : str.intern();
	}

}

package recyclednumbers;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;

/**
 * @author Emmanuel JS Hoarau - Google Code Jam 2012
 */
public class RecycledNumbers {
    
    String executeLine(final String line) {
        String words[] = line.split(" ");
        String sa = words[0], sb = words[1];
        int a = Integer.parseInt(sa);
        int b = Integer.parseInt(sb);
        
        int result = 0;
        for (int n = a; n <= b; ++n) {
            HashSet<String> matches = new HashSet<String>();
            String s = Integer.toString(n);
            String ss = s + s.substring(0, s.length()-1);
            int l = s.length();
            for (int j = 1; j < l; ++j) {
                if (ss.charAt(j) != '0') {
                    String r = ss.substring(j, j+l);
                    if ((r.compareTo(s) > 0) && (r.compareTo(sa) >= 0) && (r.compareTo(sb) <= 0) && !matches.contains(r)) {
                        matches.add(r);
                        ++result;
                    }
                }
            }
        }
        
        return Integer.toString(result);
    }

    void execute() {
        try {
            InputStreamReader stream = new InputStreamReader(System.in);
            BufferedReader reader = new BufferedReader(stream);
        
            String line = reader.readLine();
            int lines = Integer.parseInt(line);
            for (int i = 1; i <= lines; ++i) {
                line = reader.readLine();
                StringBuilder out = new StringBuilder();
                out.append("Case #");
                out.append(i);
                out.append(": ");
                out.append(executeLine(line));
                System.out.println(out.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

