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

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

import b.Dancing;

public class Recycled {
	public static void main(String[] args) throws IOException {
//		System.out.println(new Recycled().solve(1111, 2222));
		BufferedReader br = new BufferedReader(new FileReader("src/c/C-small-attempt0.in"));
		BufferedWriter bw = new BufferedWriter(new FileWriter("src/c/out"));
		
		int row = Integer.parseInt(br.readLine());
		System.out.println("ROW : " + row);
		
		Recycled rec = new Recycled();
		
		for(int i=0; i<row; i++) {
			String[] line = br.readLine().split(" ");
			
			String res = "Case #" + (i+1) + ": " + rec.solve(Integer.parseInt(line[0]), Integer.parseInt(line[1]));
			bw.write(res + "\n");
			System.out.println(res);
		}
		
		bw.close();
		br.close();
	}

	public int solve(int a, int b) {
		int cnt = 0;
		for(int i=a; i<=b; i++) {
			for(int j=i; j<=b; j++) {
				if (recycled(i,j)) cnt++;
			}
		}
		return cnt;
	}

	private boolean recycled(int in, int im) {
		if (im<10 || in<10 || im == in) return false;
		
		String n = String.valueOf(in);
		String m = String.valueOf(im); 
		
		if (n.length() < m.length()) {
			n = "00000000000".substring(0, m.length() - n.length()) + n;
		}

		if (checkDigits(n, m)) {
//			System.out.print(n + "\t" + m + "\t");
			if (checkRotate(n, m)) {
//				System.out.println(n + "\t" + m);
//				System.out.println("X");
				return true;
			}
			else {
//				System.out.println();
				return false;
			}
		}
		
		return false;
	}
	
	private boolean checkRotate(String n, String m) {
		int len = n.length();
		for(int i=len-1; i>0; i--) {
			boolean correct = true;
			for(int j=0;j<len;j++) {
				int ni = (i+j >= len) ? (i+j-len) : i+j;
//				System.out.print("\n- " + ni + " " + j);
				if (n.charAt(ni) != m.charAt(j)) {
					correct = false;
					break;
				}
			}
			if (correct == true) return true;
		}
		return false;
	}

	int d[] = new int[10];
	
	private boolean checkDigits(String n, String m) {
		clearD();
		
		for(int i=0; i<n.length(); i++) {
			d[n.charAt(i)-'0']++;
			d[m.charAt(i)-'0']--;
		}
		
		for(int i=0; i<10; i++) {
			if (d[i] != 0) return false;
		}
		
		return true;
	}

	private void clearD() {
		for(int i=0; i<10; i++) {
			d[i] = 0;
		}
	}
}
