import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Recycled {
	public static void main(String[] args) throws Exception{
		String inputFile = "C-small-attempt0.in";
		
		BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));
		BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output")));
		long num_cases = Long.parseLong(input.readLine());
		int current_case = 0;
		
		while (current_case < num_cases){
			current_case++;
			
			String[] fields = input.readLine().split(" ");
			int A = Integer.parseInt(fields[0]);
			int B = Integer.parseInt(fields[1]);
			int total = 0;
			
			for (int n = A; n < B; n++){
				for (int m = n+1; m <= B; m++){
					if (isRecycled(n,m)) total++;
				}
			}
			
			System.out.println("Case #" + current_case + ": " + total);
			output.write("Case #" + current_case + ": " + total + "\n");
		}
		output.close();
	}

	private static boolean isRecycled(int n, int m) {
		String sn = ""+n;
		String sm = ""+m;
		if (sn.length() != sm.length()) return false;
		int totaln = 0, totalm = 0;
		for (int i = 0; i < sn.length(); i++){
			totaln += sn.charAt(i);
			totalm += sm.charAt(i);
		}
		if (totaln != totalm) return false;
		for (int i = 0; i < sn.length()-1; i++){
			sm = sm.charAt(sm.length() - 1) + sm.substring(0, sm.length() - 1);
			if (sm.equals(sn)) return true;
		}
		return false;
	}
}

package codejam;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;

public class RecycledNumbers {

	/**
	 * @param args
	 */

	public boolean isRecycled(char[] f, char[] s) {
		// f.length == s.length;
		char firstChar = f[0];
		int len = f.length;
		boolean match = false;
		for (int i = 0; i < len && !match; i++) {
			if (s[i] == firstChar) {
				int k = i;
				match = true;
				for (int j = 0; j < len && match; j++, k++) {
					if (f[j] != s[k % len]) {
						match = false;
					}
				}
			}
		}
		return match;
	}

	public int getMaxRecycle(int min, int max) {
		int count = 0;
		char[] f;
		char[] s;
		for (int i = min; i < max; i++) {
			f = ("" + i).toCharArray();
			for (int j = i + 1; j < max; j++) {
				s = ("" + j).toCharArray();
				if (f.length == s.length) {
					if (isRecycled(f, s)) {
						count++;
					}
				}
			}
		}
		return count;
	}

	// Input
	// 4
	// 1 9
	// 10 40
	// 100 500
	// 1111 2222
	//
	// Output
	// Case #1: 0
	// Case #2: 3
	// Case #3: 156
	// Case #4: 287

	public static void main(String[] args) {
		RecycledNumbers r = new RecycledNumbers();
		try {
			FileInputStream fstream = new FileInputStream("C-small-attempt0.in");
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));

			FileWriter fwriter = new FileWriter("C-small-attempt0.out");
			BufferedWriter out = new BufferedWriter(fwriter);

			String strLine;
			strLine = br.readLine();
			int testCases = Integer.parseInt(strLine);

			int currentCase = 0;
			String[] input;
			int min, max;
			long start = System.currentTimeMillis();
			while ((strLine = br.readLine()) != null && currentCase < testCases) {
				input = strLine.split(" ");
				min = Integer.parseInt(input[0]);
				max = Integer.parseInt(input[1]);
				if (min > max) {
					min += max;
					max = min - max;
					min -= max;
				}
				out.write("Case #" + ++currentCase + ": "
						+ r.getMaxRecycle(min, max));

				out.newLine();
			}
			System.out.println("Time is: "
					+ (System.currentTimeMillis() - start) * 10E-3 + " Sec.");
			in.close();
			out.close();
		} catch (Exception e) {
			System.err.println("I/O Error while reading file.");
		}
	}
}
