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

class RecycledNumbers{
	
	
	
	int getcount(int small,int large){
		int count = 0;
		for(int currnum = small; currnum < large; currnum++){
			int permut = currnum;
			int no_of_digits = 0;
			while(permut > 0){
				permut = permut/10;
				no_of_digits++;
			}
			int lastDigit = currnum % 10;
			permut = currnum / 10;
			permut = permut + lastDigit * (int)Math.pow(10, no_of_digits-1);
			while(permut != currnum){
				
				if(permut >= currnum && permut <= large){
					
					count++;
				}
				lastDigit = permut % 10;
				permut = permut / 10;
				permut = permut + lastDigit * (int)Math.pow(10, no_of_digits-1);
				
			}
			
		}
		
		return count;
	}
	
	public static void main(String args[]){
		RecycledNumbers d = new RecycledNumbers();
		
		int no_of_cases = 0;
		String input = "";
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try{
			no_of_cases = Integer.parseInt(br.readLine());
		}
		catch(Exception e){
			System.out.println("First line is not a number");
		}
		
		for(int i=1; i <= no_of_cases;i++){
			try{
				input = br.readLine();
			}
			catch(Exception e){
				System.out.println("Number of test cases different from the number mentioned in first line");
			}
			
			System.out.print("Case #" +i+": ");
			
			StringTokenizer t =  new StringTokenizer(input);
			int small=0,large=0;
			if(t.hasMoreTokens()){
				small = Integer.parseInt(t.nextToken());
			}
			if(t.hasMoreTokens()){
				large = Integer.parseInt(t.nextToken());
			}
			System.out.println(d.getcount(small,large));
			
		}
		
		
		

	}
	
	
}
package com.google.recyclednumbers;

import java.util.Scanner;

import com.google.util.FileUtil;

public class RecycledNumbers {

	public static void main(String[] args) {
		String orgFilePath = "E:\\Google_Jam\\RecycledNumbers\\C-small-attempt0.in";
		Scanner scanner = FileUtil.openFile(orgFilePath);
		StringBuilder sb = new StringBuilder();
		int caseNum = Integer.valueOf(scanner.nextLine());
		for (int i = 0; i < caseNum; i++) {
			sb.append("Case #" + (i + 1) + ": ");
			int n = 0;
			 int a = scanner.nextInt();
			 int b = scanner.nextInt();
			/*int a = 1111;
			int b = 2222;*/
			if (b <= 20) {
				n = 0;
				sb.append(n);
				sb.append("\n");
				continue;
			} else {
				for (int k = a; k <= b; k++) {
					String left = String.valueOf(k);
					int length = left.length();// ×ó±ßÊýµÄÎ»Êý
					int temp = 0;// ÓÃÓÚ¼ÇÂ¼ÄÜ×ª»»³öÀ´µÄÊýµÄ¸öÊý
					int[] tempCase = new int[20];
					for (int j = 0; j < length; j++) {
						String sub1 = left.substring(0, j + 1);
						String sub2 = left.substring(j + 1, length);
						String newNum = sub2 + sub1;
						int right = Integer.valueOf(newNum);			
						if (right <= b && k < right) {
							if (temp == 0) {
								n++;
								tempCase[temp] = right;
								temp++;
							} else {
								int p;
								for (p = 0; p < temp && tempCase[p] != right; p++);
								if (p == temp) {
									tempCase[temp] = right;
									temp++;
									n++;
								}
							}
							//System.out.println(k + "&&" + right);
							//sb.append(k + "&&" + right);
							//sb.append("\n");
						}
					}
				}
			}
			sb.append(n);
			sb.append("\n");
		}
		FileUtil.writeFile(sb, "RecycledNumbers\\small.in");
	}
}
