/*
 * 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 info.m3o.gcj2012.recyclednumber;

public class RecycledNumber {
	protected String numStr;
	protected int numDigits;

	protected int minA, maxB;

	public RecycledNumber(String s, int mina, int maxb){
		numStr = new String(s);
		numDigits = s.length();
		minA = mina;
		maxB = maxb;
	}

	//	public RecycledNumber(String s){
	//		numStr = new String(s);
	//		numDigits = s.length();
	//	}

	public RecycledNumber(int p, int mina, int maxb){
		numStr = new String(String.valueOf(p));
		numDigits = numStr.length();
		minA = mina;
		maxB = maxb;
	}

	//	public RecycledNumber(int p){
	//		numStr = new String(String.valueOf(p));
	//		numDigits = numStr.length();
	//	}

	public static String recycleAt(String s, int i){
		assert ((i>0) && (i<s.length())) : "int parameter is greater than length.";
		return ( new String (s.substring(i) + s.substring(0, i)));
	}

	public static String[] getCycledNumber(String s){
		String [] str;
		str = new String[s.length()-1];
		
		for(int i=1; i < s.length(); i++){
		 // str[i-1] = new String (recycleAt(s, i));
			str[i-1] = recycleAt(s, i);
		}
		return str;
	}
	

	public void addRecycledNumbers(MyNumberStringPairSet recycledNumberSet,
			MyNumberStringSet tested,  String sNum2) {
// TODO
		/*
		if (tested.contains(sNum2) == true){
			return;
		}*/

		int intS2 = Integer.valueOf(sNum2);

		if (((this.numStr.compareTo(sNum2)) != 0) && (sNum2.startsWith("0") == false)
				&& (intS2 >= this.minA) && (intS2 <= this.maxB)) {
			recycledNumberSet.add(new MyNumberStringPair(this.numStr, sNum2));
		}
		tested.add(numStr);
		tested.add(sNum2);

	}			


	
	
	public void addRecycledNumbers(MyNumberStringPairSet recycledNumberSet, MyNumberStringSet tested){
		if (tested.contains(numStr) == true){ // This value has been tested already.
			return;  
		}

		for(int i=1; i < this.numDigits; i++){
			String s = new String (recycleAt(numStr, i));
			
			// HERE TODO
			tested.add(s);
			int intS = Integer.valueOf(s);
			
			if ((s.startsWith("0")==false) 
					&& (numStr.compareTo(s) != 0)
					&& (intS >=this.minA) 
					&& (intS <= this.maxB)){
				recycledNumberSet.add(new MyNumberStringPair(s, numStr));
			}
			
//			if (s.startsWith("0")== false){
//				tested.add(s); // Here is the part
//			}
		}
		tested.add(numStr);		

	}



	public static boolean testrecycleAt(){
		boolean retb = false;
		boolean []b = new boolean[4];

		String str1 = "12345";

		String str2 = recycleAt(str1, 1);
		b[0] = ("23451".compareTo(str2)==0);

		str2 = recycleAt(str1, 2);
		b[1] = ("34512".compareTo(str2) == 0);

		str2 = recycleAt(str1, 3);
		b[2] = ("45123".compareTo(str2) == 0);

		str2 = recycleAt(str1, 4);
		b[3] = ("51234".compareTo(str2) == 0);

		retb = (b[0] && b[1] && b[2] && b[3]);
		assert (retb == true) : "something is wrong with recycleAt method.";
		return retb;
	}

	public String getNumStr() {
		return numStr;
	}
	public void setNumStr(String numStr) {
		this.numStr = numStr;
	}
	public int getNumDigits() {
		return numDigits;
	}
	public void setNumDigits(int numDigits) {
		this.numDigits = numDigits;
	}



}
