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

}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.HashSet;

public class ProblemB {

	static int A;
	static int B;
	static HashSet<Integer> set;

	public static void main(String[] args) {

		try {
			/* Reading */
			FileInputStream fstream = new FileInputStream("input.txt");
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));

			/* Writing */
			FileWriter fw = new FileWriter(new File("output"));
			BufferedWriter bw = new BufferedWriter(fw);

			String strLine;

			Integer T = 0;
			int caseCounter = 0;

			while ((strLine = br.readLine()) != null) {
				if (caseCounter == 0) {
					T = new Integer(strLine);
				} else {
					set = new HashSet<Integer>();
					int index =strLine.indexOf(' ');
					A = new Integer(strLine.substring(0, index).trim());
					B = new Integer(strLine.substring(index).trim());

					int nbRecycledPairs = 0;
					for (int i = A; i <= B; i++) {
						if (!set.contains(i)) {
							nbRecycledPairs += findRecycled(i);
						}
					}

					String toPrint;
					if (caseCounter == T) {
						toPrint = "Case #" + caseCounter + ": "
								+ nbRecycledPairs;
					} else {
						toPrint = "Case #" + caseCounter + ": "
								+ nbRecycledPairs + "\n";
					}
					System.out.print(toPrint);
					bw.write(toPrint);
				}
				caseCounter++;
			}
			in.close();
			bw.close();
		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}

	}

	static int findRecycled(int n) {
		String s = Integer.toString(n);
		int nbRecycled = 0;
		int nbRecycledPairs = 0;
		for (int i = 0; i < s.length(); i++) {
			String t = s.substring(i) + s.substring(0, i);
			if (t.charAt(0) != '0' && isInSet(t) && !t.equals(s)
					&& !set.contains(new Integer(t))) {
				set.add(new Integer(t));
				nbRecycled++;
			}

		}
		if (nbRecycled != 0) {
			nbRecycled++;
		}
		nbRecycledPairs = nbRecycled * (nbRecycled - 1) / 2;

		return nbRecycledPairs;
	}

	static boolean isInSet(String n) {
		int i = new Integer(n);
		if (i >= A && i <= B) {
			return true;
		}
		return false;
	}
}
