/*
 * 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 com.codejam2012;

import java.util.TreeMap;

import com.codejam2012.CodeJam1.Case;



public class CodeJam3 {

	public static String fileDir = "P:\\CodeJam2012\\";
	public static String baseFileName = "C-small-attempt2";
	public static String inputFileName = fileDir + baseFileName + ".in";
	public static String outputFileName = fileDir + baseFileName + ".out";

	public static int Tcase;
	public static int Ccase = 0;
	public static String[] cases;
	public static Case singleCase;
	
	public static void main(String[] args) {
		
		FileUtils.deleteFile(outputFileName);
		String inputString = FileUtils.readFile(inputFileName);
		parseInputString(inputString);
		for (; Ccase < Tcase;) {
			parse();
		}

	}
	
	public static void parseInputString(String inputString) {

		cases = inputString.split("\n");
		Tcase = Integer.parseInt(cases[0]);
		Ccase = 0;
	}

	private static void parse() {

		Ccase++;
		FileUtils.appendToFile(outputFileName, "Case #" + Ccase + ": ");
		singleCase = new Case(cases[Ccase]);
		singleCase.execute();
		FileUtils.appendToFile(outputFileName, singleCase.getDataToPrint()
				+ "\n");

	}

	public static class Case {
		StringBuilder outputStatement;
		
		int N;
		int M;

		public Case(String caseString) {
			outputStatement = new StringBuilder();
			String[] arr = caseString.split(" " );
			N = Integer.parseInt(arr[0]);
			M = Integer.parseInt(arr[1]);
		}

		public void execute() {
			
			int noOfDigits = (N + "").length();
			int TCount = 0;
			TreeMap<Integer, Integer> mList = new TreeMap<Integer,Integer>();
			

			for (int n = N; n <= M; n++) {

				for (int r = 1; r < noOfDigits; r++) {
					int m = rotater(n, r, noOfDigits);
					if (m <= M && m >= N) {
						if (m > n) {
							 if(mList.containsKey(n) ){
								 if(mList.get(n) == m){
									 
								 }else{
									 TCount++;
										mList.put(n,m);
								 }
							 }else if (mList.containsKey(m) ){
								 if(mList.get(m) == n){
									 
								 }else{
									 TCount++;
										mList.put(n,m);
								 }
							 }else{
								 TCount++;
									mList.put(n,m);
							 }
							
						}
					}

				}

			}
			
			outputStatement.append(TCount);
		}
		
		public static int rotater(int n, int rotate, int noOfDigits) {
			int base = (int) Math.pow(10, noOfDigits - 1);
			int[] digits = new int[noOfDigits];
			int d = 0;
			int ans = 0;
			for (int i = base; i >= 1; i /= 10) {
				digits[d] = n / i;
				d++;
				n = n % i;
			}

			shiftLeft(digits, rotate);
			d = 0;
			for (int i = base; i >= 1; i /= 10) {
				ans += digits[d] * i;
				d++;
			}

			return ans;

		}

		public static void shiftLeft(int[] array, int amount) {

			for (int j = 0; j < amount; j++) {
				int a = array[0];
				int i;
				for (i = 0; i < array.length - 1; i++)
					array[i] = array[i + 1];
				array[i] = a;
			}

		}

		public String getDataToPrint() {
			return outputStatement.toString();

		}

	}


}
