/*
 * Problem C. Recycled Numbers
 * 
 * Do you ever become frustrated with television because you keep seeing the
 * same things, recycled over and over again? Well I personally don't care about
 * television, but I do sometimes feel that way about numbers.
 * 
 * Let's say a pair of distinct positive integers (n, m) is recycled if you can
 * obtain m by moving some digits from the back of n to the front without
 * changing their order. For example, (12345, 34512) is a recycled pair since
 * you can obtain 34512 by moving 345 from the end of 12345 to the front. Note
 * that n and m must have the same number of digits in order to be a recycled
 * pair. Neither n nor m can have leading zeros.
 * 
 * Given integers A and B with the same number of digits and no leading zeros,
 * how many distinct recycled pairs (n, m) are there with A â¤ n < m â¤ B?
 * 
 * Input
 * 
 * The first line of the input gives the number of test cases, T. T test cases
 * follow. Each test case consists of a single line containing the integers A
 * and B.
 * 
 * Output
 * 
 * For each test case, output one line containing "Case #x: y", where x is the
 * case number (starting from 1), and y is the number of recycled pairs (n, m)
 * with A â¤ n < m â¤ B.
 * 
 * Limits
 * 
 * 1 â¤ T â¤ 50.
 * 
 * A and B have the same number of digits.

Small dataset

1 â¤ A â¤ B â¤ 1000.

Large dataset

1 â¤ A â¤ B â¤ 2000000.

Sample


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

package google.codejam.y2012.qualifications;

import google.codejam.commons.Utils;

import java.io.IOException;
import java.util.StringTokenizer;

/**
 * @author Marco Lackovic <marco.lackovic@gmail.com>
 * @version 1.0, Apr 14, 2012
 */
public class RecycledNumbers {

	private static final String INPUT_FILE_NAME = "C-large.in";
	private static final String OUTPUT_FILE_NAME = "C-large.out";

	public static void main(String[] args) throws IOException {
		String[] input = Utils.readFromFile(INPUT_FILE_NAME);
		String[] output = new String[input.length];
		int x = 0;
		for (String line : input) {
			StringTokenizer st = new StringTokenizer(line);
			int a = Integer.valueOf(st.nextToken());
			int b = Integer.valueOf(st.nextToken());
			System.out.println("Computing case #" + (x + 1));
			output[x++] = "Case #" + x + ": " + recycledNumbers(a, b);
		}
		Utils.writeToFile(output, OUTPUT_FILE_NAME);
	}

	private static int recycledNumbers(int a, int b) {
		int count = 0;
		for (int n = a; n < b; n++) {
			count += getNumGreaterRotations(n, b);
		}
		return count;
	}

	private static int getNumGreaterRotations(int n, int b) {
		int count = 0;
		char[] chars = Integer.toString(n).toCharArray();
		for (int i = 0; i < chars.length; i++) {
			chars = rotateRight(chars);
			if (chars[0] != '0') {
				int m = Integer.valueOf(new String(chars));
				if (n == m) {
					break;
				} else if (n < m && m <= b) {
					count++;
				}
			}
		}
		return count;
	}

	private static char[] rotateRight(char[] chars) {
		char last = chars[chars.length - 1];
		for (int i = chars.length - 2; i >= 0; i--) {
			chars[i + 1] = chars[i];
		}
		chars[0] = last;
		return chars;
	}

}

package qualification;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;


public class C {

	static byte[][] MAP = new byte[1000][1000];
	
	private static final char[] CASE = "Case #".toCharArray();
	
	public static void main(String[] args) throws Throwable {
//		in = System.in;
//		out = System.out;
//		in = new FileInputStream("in/C-small-attempt0.in");
		in = new FileInputStream("in/C-large.in");
		out = new PrintStream(new FileOutputStream("out/C.out"));

		
		int T = readInt();
		
		for (int test = 1; test <= T; test++) {
			int A = readInt();
			int B = readInt();
			
			int result = 0;
			
			for (int n = A; n < B; n++) {
				int nLen = length(n);
				
				// find all possible recycled numbers for this one
				for (int i = 1; i < nLen; i++) {
					int tenpow = pow(10,i);
					int tenpowfront = pow(10, nLen-i);
					int m = (n/tenpow) + ((n%tenpow) * tenpowfront);
					if (m == n) break;// if we found n back, it is a circular number (ex: 123123), and if we keep going, we'll duplicate the results found
					// the recycled numbers have to be: n < m; m <= B
					if (n < m && m <= B) {
						result++;
					}
				}
			}
			
			writeOut(CASE);
			writeOutNumber(test);
			writeOut(':');
			writeOut(' ');
			writeOutNumber(result);
			writeOutLn();
			flush();
		}
		
		flush();

	}
	
	static int length(int num) {
		int len = 1;
		int powerTen = 10;
		while (num >= powerTen) {
			powerTen *= 10;
			len++;
		}
		return len;
	}

	static InputStream in;
	static PrintStream out;
	
	static byte[] buffer = new byte[8192];
	static int pos;
	static int max;

	static final int maxOut = 8192;
	static byte[] output = new byte[maxOut];
	static int posOut = 0;
	
	static char[] tmpcharr = new char[128];

	static int pow2(int v) {
		return v * v;
	}
	
	static int pow(int v, int mag) {
		return (int) Math.pow(v, mag);
	}

	static boolean reachedEndOfLine() throws Throwable {
		while (true) {
			int data = read();
			if (data == -1)
				return true;
			if (data == '\r' || data == '\n') {
				return true;
			}
			if (data == ' ') {
				continue;
			}
			pos--;
			return false;
		}
	}

	// COMPARISION FUNCTIONS

	static boolean isValidChar(int v) {
		// nao verifica o 'DELETE' (127)
		return (v & 0xE0) != 0;
	}

	static boolean isNum(int v) {
		return v >= '0' && v <= '9';
	}

	static boolean isLowercase(int cur) {
		return cur >= 'a' && cur <= 'z';
	}

	static boolean isUppercase(int cur) {
		return cur >= 'A' && cur <= 'Z';
	}

	// is a letter (alphabetic)
	static boolean isAlpha(int cur) {
		return isLowercase(cur) || isUppercase(cur);
	}

	// is alphanumeric (letters and numbers)
	static boolean isAlphaNum(int cur) {
		return isAlpha(cur) || isNum(cur);
	}

	static boolean isMinus(int cur) {
		return cur == '-';
	}

	static boolean isPlus(int cur) {
		return cur == '+';
	}

	// INPUT FUNCTIONS

	static void goToBeginOfNextWord() throws Throwable {
		int cur;
		// se for -1 já sai do método
		while ((cur = read()) != -1) {
			// se for um char válido então deixa o ponteiro nele
			if (cur > 32) {
				pos--;
				return;
			}
		}
	}

	// return the length of the string read
	static String readWord() throws Throwable {
		int len = 0;
		int cur;
		// descarta os espacos em branco primeiro
		while ((cur = read()) < 33) {
			if (cur == -1) return "";
		}

		tmpcharr[len++] = (char) cur;
		while ((cur = (char) read()) > 32) {
			tmpcharr[len++] = (char) cur;
		}

		return new String(tmpcharr, 0, len);
	}

	static void discardWord() throws Throwable {
		int cur;
		// descarta os espacos em branco primeiro
		while ((cur = read()) < 33) {
			if (cur == -1)
				return;
		}
		// descarta os valores 'validos'
		while ((cur = read()) > 32)
			;
	}

	static int readInt() throws Throwable {
		int cur;
		while (!isNum(cur = read()) && !isMinus(cur))
			if (cur == -1)
				return -1;
		int v = 0;
		boolean minus = false;
		if (isMinus(cur)) {
			minus = true;
			cur = read();
		}
		while (isNum(cur)) {
			v *= 10;
			v += (cur - '0');
			cur = read();
		}
		return minus ? -v : v;
	}

	static char readChar() throws Throwable {
		int cur;
		for (;;) {
			cur = read();
			if (cur == -1)
				return 0;
			if (cur == '\r' || cur == '\n' || cur == ' ')
				continue;

			return (char) cur;
		}
	}

	static long readLong() throws Throwable {
		int cur;
		while (!isNum(cur = read()) && !isMinus(cur))
			if (cur == -1)
				return -1;
		long v = 0;
		boolean minus = false;
		if (isMinus(cur)) {
			minus = true;
			cur = read();
		}
		while (isNum(cur)) {
			v *= 10;
			v += (cur - '0');
			cur = read();
		}
		return minus ? -v : v;
	}

	static boolean hasInput() throws Throwable {
		while (true) {
			int data = read();
			if (data == -1)
				return false;
			if ((data & 0xE0) != 0) {
				pos--;
				return true;
			}
		}
	}

	static int read() throws Throwable {
		if (pos >= max) {
			max = in.read(buffer);
			if (max < 0)
				return -1;
			pos = 0;
		}
		return buffer[pos++] & 0xff;
	}

	// OUTPUT FUNCTIONS

	// escreve um char

	static void writeOut(char c) {
		if (posOut >= maxOut) {
			writeOutToStream();
		}
		output[posOut++] = (byte) c;
	}

	// escreve a quebra de linha
	static void writeOutLn() {
		writeOut('\n');
	}

	// escreve um char[]
	static void writeOut(char[] cs) {
		writeOut(cs, 0, cs.length);
	}

	// escreve uma cadeia de caracteres
	static void writeOut(char[] chars, int offset, int length) {
		final int end = offset + length;
		for (int i = offset; i < end; i++) {
			writeOut(chars[i]);
		}
	}

	static void writeOutNumber(int value) {
		int len = 0;
		int pos = tmpcharr.length;
		if (value == 0) {
			writeOut('0');
			return;
		}
		boolean negative = value < 0;
		if (negative)
			value = -value;
		while (value > 0) {
			tmpcharr[--pos] = (char) ((value % 10) + '0');
			len++;
			value /= 10;
		}
		if (negative)
			writeOut('-');
		writeOut(tmpcharr, pos, len);
	}

	static void writeOutNumberAsBin(int num) {
		int len = 0;
		int pos = tmpcharr.length;
		if (num == 0) {
			writeOut('0');
			return;
		}
		while (num > 0) {
			tmpcharr[--pos] = ((num & 1) == 1) ? '1' : '0';
			len++;
			num >>>= 1;
		}
		writeOut(tmpcharr, pos, len);
	}

	static void flush() {
		writeOutToStream();
		out.flush();
	}

	// PRIVATE methods must be used only by the utility methods

	private static void writeOutToStream() {
		out.write(output, 0, posOut);
		posOut = 0;
	}

}