/*
 * 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;
	}

}

import java.util.ArrayList;
import java.io.IOException;


public class exo2 {
	
	public static ArrayList<Integer> listeCouple = new ArrayList<Integer>() ;

	public static int T;
	public static int A ;
	public static int B ;
	public static int Result = 0 ;
	static int k ;
	static int l ;
	static int m ;
	static int n ;
	static int o ;
	static int p ;
	static int q ;
	
	public static void digit2(int A,int B,int D){
		k = D%10 ;
		l = (D-k)/10 ;
		if((k*10+l)>=A && (k*10+l)<=B && (k*10+l)>D){
			Result = Result + 1 ;
		}
	}
	public static void digit3(int A,int B,int D){
		k = D%10 ;
		l = ((D-k)%100) /10;
		m = (D-l*10-k ) /100;
		if ((k*100+m*10+l)>=A &&(k*100+m*10+l)<=B && (k*100+m*10+l)>D ) {
			Result = Result + 1 ;	

		}
		if ((l*100+k*10+m)>=A &&(l*100+k*10+m)<=B &&(l*100+k*10+m)>D ) {
			Result = Result + 1 ;
		}
	}
	public static void digit4(int A,int B,int D){
		k = D%10 ;
		l = ((D-k)%100) /10;
		m = (D-l*10-k)%1000 /100 ;
		n = (D-m*100-l*10-k)/1000;
		if ((k*1000+n*100+m*10+l)>=A && (k*1000+n*100+m*10+l)<=B &&(k*1000+n*100+m*10+l)>D ){
			Result = Result + 1 ;	
		}		
		if ((l*1000+k*100+n*10+m)>=A &&(l*1000+k*100+n*10+m)<=B &&(l*1000+k*100+n*10+m)>D ){
			Result = Result + 1 ;
		}
		if ((m*1000+l*100+k*10+n)>=A &&(m*1000+l*100+k*10+n)<=B &&(m*1000+l*100+k*10+n)>D ){
			Result = Result + 1 ;
		}
	}
	public static void digit5(int A,int B,int D){
		k = D%10 ;
		l = ((D-k)%100) /10;
		m = (D-l*10-k)%1000 /100 ;
		n = ((D-m*100-l*10-k)%10000) / 1000;
		o = (D-n*1000-m*100-l*10-k)/10000 ;
		if ((k*10000+o*1000+n*100+m*10+l)>=A && (k*10000+o*1000+n*100+m*10+l)<=B && (k*10000+o*1000+n*100+m*10+l)>D) {
			Result = Result + 1 ;
		}
		if ((l*10000+k*1000+o*100+n*10+m)>=A && (l*10000+k*1000+o*100+n*10+m)<=B && (l*10000+k*1000+o*100+n*10+m)>D ) {
			Result = Result + 1 ;
		}
		if ((m*10000+l*1000*k*100+o*10+n)>=A && (m*10000+l*1000*k*100+o*10+n)<=B && (m*10000+l*1000*k*100+o*10+n)>D){
			Result = Result + 1 ;
		}
		if ((n*10000+m*1000*l*100+k*10+o)>=A && (n*10000+m*1000*l*100+k*10+o)<=B && (n*10000+m*1000*l*100+k*10+o)>D){
			Result = Result + 1 ;
		}
	}
	public static void digit6(int A,int B, int D){
		k = D%10 ;
		l = ((D-k)%100) /10;
		m = (D-l*10-k)%1000 /100 ;
		n = ((D-m*100-l*10-k)%10000) / 1000;
		o = ((D-n*1000-m*100-l*10-k)%100000) /10000;
		p = (D-o*10000-n*1000-m*100-l*10-k)/100000 ;
		if ((k*100000+p*10000+p*1000+n*100+m*10+l)>=A && (k*100000+p*10000+p*1000+n*100+m*10+l)<=B 
				&& (k*100000+p*10000+p*1000+n*100+m*10+l)>D) {
			Result = Result + 1 ;
		}
		if ((l*100000+k*10000+p*1000+o*100+n*10+m)>=A && (l*100000+k*10000+p*1000+o*100+n*10+m)<=B 
				&& (l*100000+k*10000+p*1000+o*100+n*10+m)>D) {
			Result = Result + 1 ;
		}
		if ((m*100000+l*10000*k*1000+p*100+o*10+n)>=A && (m*100000+l*10000*k*1000+p*100+o*10+n)<=B  
				&& (m*100000+l*10000*k*1000+p*100+o*10+n)>D){
			Result = Result + 1 ;
		}
		if ((n*100000+m*10000*l*1000+k*100+p*10+o)>=A && (n*100000+m*10000*l*1000+k*100+p*10+o)<=B  
				&& (n*100000+m*10000*l*1000+k*100+p*10+o)>D){
			Result = Result + 1 ;
		}
		if ((o*100000+n*10000+m*1000*l*100+k*10+p)>=A && (o*100000+n*10000+m*1000*l*100+k*10+p)<=B  
				&& (o*100000+n*10000+m*1000*l*100+k*10+p)>D){
			Result = Result + 1 ;
		}
	}
	public static void digit7(int A,int B,int D){
		k = D%10 ;
		l = ((D-k)%100) /10;
		m = (D-l*10-k)%1000 /100 ;
		n = ((D-m*100-l*10-k)%10000) / 1000;
		o = ((D-n*1000-m*100-l*10-k)%100000) /10000;
		p = ((D-o*10000-n*1000-m*100-l*10-k)%1000000)/100000 ;
		q = (D-p*100000-o*10000-n*1000-m*100-l*10-k)/1000000 ;
		if ((k*1000000+q*100000+p*10000+o*1000+n*100+m*10+l)>=A && (k*1000000+q*100000+p*10000+o*1000+n*100+m*10+l)<=B 
				&& (k*1000000+q*100000+p*10000+o*1000+n*100+m*10+l)>D) {
			Result = Result + 1 ;
		}
		if ((l*1000000+k*100000+q*10000+p*1000+o*100+n*10+m)>=A && (l*1000000+k*100000+q*10000+p*1000+o*100+n*10+m)<=B 
				&& (l*1000000+k*100000+q*10000+p*1000+o*100+n*10+m)>D ) {
			Result = Result + 1 ;
		}
		if ((m*1000000+l*100000+k*10000+q*1000+p*100+o*10+n)>=A && (m*1000000+l*100000+k*10000+q*1000+p*100+o*10+n)<=B  
				&& (m*1000000+l*100000+k*10000+q*1000+p*100+o*10+n)>D){
			Result = Result + 1 ;
		}
		if ((n*1000000+m*100000+l*10000+k*1000+q*100+p*10+o)>=A && (n*1000000+m*100000+l*10000+k*1000+q*100+p*10+o)<=B  
				&& (n*1000000+m*100000+l*10000+k*1000+q*100+p*10+o)>D){
			Result = Result + 1 ;
		}
		if ((o*1000000+n*100000+m*10000+l*1000+k*100+q*10+p)>=A && (o*1000000+n*100000+m*10000+l*1000+k*100+q*10+p)<=B  
				&& (o*1000000+n*100000+m*10000+l*1000+k*100+q*10+p)>D){
			Result = Result + 1 ;
		}
		if ((p*1000000+o*100000+n*10000+m*1000+l*100+k*10+q)>=A && (p*1000000+o*100000+n*10000+m*1000+l*100+k*10+q)<=B  
				&& (p*1000000+o*100000+n*10000+m*1000+l*100+k*10+q)>D){
			Result = Result + 1 ;
		}
	}

	public static void traiter(int A,int B, int j) {
		int D = A ;
		if (B<10) {
			Result = 0 ;			
		}
		else if (B<=100){
			for (int i=0;i<(B-A);i++){
				digit2(A,B,D+i);
			}
		}
		else if(B<1000){
			for (int i=0;i<(B-A);i++){
				digit3(A,B,D+i);
			}
		}
		else if(B<10000){
			for (int i=0;i<(B-A);i++){
				digit4(A,B,D+i);
			}
		}
		else if(B<100000){
			for (int i=0;i<(B-A);i++){
				digit5(A,B,A+i);
			}
		}
		else if(B<1000000){
			for (int i=0;i<(B-A);i++){
				digit6(A,B,A+i);
			}
		}
		else if(B<10000000){
			for (int i=0;i<(B-A);i++){
				digit7(A,B,A+i);
			}
		}
		
	}
	public static void read(String arg)  throws java.io.IOException {
		java.util.Scanner lecteur ;		
		java.io.File fichier = new java.io.File(arg);
		lecteur = new java.util.Scanner(fichier);
	
		T = lecteur.nextInt() ;
		for (int i=0;i<=T;i++){
			if (lecteur.hasNextInt()){
				A = lecteur.nextInt();
				B = lecteur.nextInt() ;
				traiter(A,B,i);
				System.out.println("Case #"+(i+1)+": "+Result);
			}
			Result = 0 ;
			listeCouple.removeAll(listeCouple) ;
		}	
	}
	

	
	public static void main(String[] args) throws IOException{
		String arg = null;
		arg = "C-large.in" ;
		read(arg) ;
	}
}