package qual;

import java.util.Scanner;

public class RecycledNumbers {

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

	}

	private void run() {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int t = 0; t < T; t++) {
			int A = sc.nextInt();
			int B = sc.nextInt();
			int result = solve(A, B);
			System.out.printf("Case #%d: %d\n", t + 1, result);
		}
	}

	public int solve(int A, int B) {
		int result = 0;
		for (int i = A; i <= B; i++) {
			int dig = (int) Math.log10(A/*same as B*/) + 1;
			int aa = i;
			for (int d = 0; d < dig - 1; d++) {
				aa = (aa % 10) * (int)Math.pow(10, dig - 1) + (aa / 10);
				if (i == aa) {
					break;
				}
				if (i < aa && aa <= B) {
//					System.out.printf("(%d, %d) ", i, aa);
					result++;
				}
			}
		}
		return result;
	}

}

package google.codejam.com;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.StringTokenizer;

public class Solution {

	public static void main(String[] args) {

		BufferedReader f = null;
		PrintWriter out = null;
		try {
			f = new BufferedReader(new FileReader("in.txt"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			out = new PrintWriter(new BufferedWriter(new FileWriter("out.txt")));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {

			// SOLUTION STARTS HERE

			int count = new Integer(f.readLine());
			for (int i = 1; i <= count; i++) {
				long sum = 0L;
				String[] in = f.readLine().split(" ");
				int from = new Integer(in[0]);
				int to = new Integer(in[1]);

				for (int num = from; num <= to; num++) {
					String toStr = new Integer(to).toString();
					String numStr = new Integer(num).toString();
					sum += getSum(numStr, toStr);

				}

				out.write("Case #" + i + ": " + sum + "\n");
				System.out.println("Case #" + i + ": " + sum);

			}

			// SOLUTION ENDS HERE

			out.flush();
			out.close();
			f.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static long getSum(String numStr, String toStr) {
		String conn = "";
		conn = numStr + numStr;
		HashSet<String> set = new HashSet<String>();
		for (int i = 0; i < numStr.length(); i++) {
			String cand = conn.substring(i, i + numStr.length());
			if(cand.compareTo(numStr) > 0 && cand.compareTo(toStr) <= 0)
				set.add(cand);
		}
		
		return set.size();
	}

}
