/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gcj;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author daniele
 */
public class GCJ_C {
    
    public static void main(String[] args) throws Exception{
        
        
        
        Scanner in = new Scanner(new File(args[0]));
        FileWriter out = new FileWriter("/home/daniele/Scrivania/Output");
        

        int prove=in.nextInt();
        int min,max;
        int cifre;
        int cifreaus;
        String temp;
        int aus;
        int count=0;
        ArrayList<Integer> vals = new ArrayList<Integer>();
        int jcount=0;
        ArrayList<Integer> perm = new ArrayList<Integer>();
        
        out.write("");
        out.flush();
        
        for(int i=1;i<=prove;i++){
            
            out.append("Case #" +i+": ");out.flush();
            
            min=in.nextInt();
            max=in.nextInt();
            
            for(int j=min;j<=max;j++){
               if(!vals.contains(j)){
                    temp=""+j;
                    cifre=temp.length();
                    aus=j;
                    perm.add(j);
                    for(int k=1;k<cifre;k++){
                        aus=rotateOnce(aus);
                        temp=""+aus;
                        cifreaus=temp.length();//elusione zeri iniziali
                        if(aus>=min && aus<=max && aus!=j && cifreaus==cifre && nobody(vals,perm)){ 
                            perm.add(aus);
                            jcount ++;
                            
                        }
                        
                    }
                    while(jcount>0){count+=jcount; jcount--;}
                    vals.addAll(perm);
                    perm= new ArrayList<Integer>();
                    
                }
            }
            out.append(count+"\n");out.flush();
            count=0;
            vals= new ArrayList<Integer>();
        }
        
        
        
    }
        static public int rotateOnce(int n){
            String s=""+n;
            if(s.length()>1){
                s=s.charAt(s.length()-1) + s.substring(0,s.length()-1);
                n=Integer.parseInt(s);
            }
            return n;
        }    
        
        static public boolean nobody(ArrayList<Integer> v,ArrayList<Integer> a){;
            for(int i=0;i<a.size();i++)
                if(v.contains(a.get(i))) return false;
            return true;
        } 
    
}


package QualificationRound2012.C.RecycledNumbers;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class RecycledNumbers {
	static final String _DIR = "src/QualificationRound2012/C/RecycledNumbers/";
	static final String INPUT_FILE = _DIR + "C-small-attempt0.in";
	static final String OUTPUT_FILE = INPUT_FILE + ".out";

	private static int algorithm(final String line) {
		int distinctPairs = 0;
		final String[] split = line.split(" ");
		final String A_str = split[0];
		final String B_str = split[1];
		final int A = Integer.parseInt(A_str);
		final int B = Integer.parseInt(B_str);

		// A â¤ n < m â¤ B
		for (int m = A; m <= B; m++) {
			for (int n = m + 1; n <= B; n++) {
				// System.out.print(A + " â¤ " + m + " < " + n + " â¤ " + B);
				if (isRecycled(m, n)) {
					// System.out.println(" --> YES!");
					distinctPairs++;
				} else {
					// System.out.println();
				}
			}
		}
		return distinctPairs;
	}

	private static boolean isRecycled(final int m, final int n) {
		final String mString = String.valueOf(m);
		final String nString = String.valueOf(n);
		final int length = mString.length();
		String rot = mString;
		for (int i = 0; i < length; i++) {
			if (rot.equals(nString)) {
				return true;
			}
			rot = rotate(rot);
		}
		return false;
	}

	public static void main(final String args[]) throws IOException {
		final BufferedWriter writer = new BufferedWriter(new FileWriter(OUTPUT_FILE));
		final BufferedReader reader = new BufferedReader(new FileReader(new File(INPUT_FILE)));

		long timeStart = System.currentTimeMillis();

		String line;
		int testcase = 0;
		while ((line = reader.readLine()) != null) {
			if (testcase != 0) {
				System.out.println("Case #" + testcase + ": " + algorithm(line));
			}
			testcase++;
		}

		long timeEnd = System.currentTimeMillis();
		System.out.println(timeEnd - timeStart + " millis");

	}

	private static String rotate(final String number) {
		final char[] numberArray = number.toCharArray();
		final char last = numberArray[numberArray.length - 1];
		return last + number.substring(0, number.length() - 1);
	}
}
