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

import java.io.BufferedReader;
import java.io.FileReader;

public class Gcj {
	private static final String path = "/home/me/dev/gcj/";
	private BufferedReader br = null;
	private String[] tokens;
	private int itoken,ntokens;
	
	static public void err(String e) {
		System.out.println("\n**\n"+e+" :-)\n");
		System.exit(1);
	}
	
	public Gcj(String filename) {
		try {
			br = new BufferedReader(new FileReader(path + filename));
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String rawLine() {
		String line = null;
		
		try {
			line = br.readLine();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return line;
	}
	
	
	public String[] readLine() {
		
		String line = rawLine();
		
		if (line == null) {
			tokens = null;
			ntokens = 0;
		}
		else {
			tokens = line.trim().split("\\s+");
			ntokens = tokens.length;
		}
		itoken = 0;
		
		return tokens;
	}
	
	private String nextToken() {
		String err = null;
		if (ntokens < 1) err = "There is no token";
		else if (itoken >= ntokens) err = "No more token";
		else {
			return tokens[itoken++];
		}
		
		Gcj.err(err);
		return null;
	}
	
	public String sToken() {
		return nextToken();
	}

	public String[] sBunch() {
		String[] l = new String[ntokens-itoken];
		int i=0;
		while (itoken < ntokens) l[i++] = tokens[itoken++];
		return l;
	}

	public char cToken() {
		return nextToken().charAt(0);
	}

	public char[] cBunch() {
		char[] l = new char[ntokens-itoken];
		int i = 0;
		while (itoken < ntokens) l[i++] = tokens[itoken++].charAt(0);
		return l;
	}

	public int iToken() {
		return Integer.parseInt(nextToken());
	}

	public int[] iBunch() {
		int[] l = new int[ntokens-itoken];
		int i = 0;
		while (itoken < ntokens) l[i++] = Integer.parseInt(tokens[itoken++]);
		return l;
	}
	
	public long lToken() {
		return Long.parseLong(nextToken());
	}

	public long[] lBunch() {
		long[] l = new long[ntokens-itoken];
		int i = 0;
		while (itoken < ntokens) l[i++]  = Long.parseLong(tokens[itoken++]);
		return l;
	}
	
	public double dToken() {
		return Double.parseDouble(nextToken());
	}

	public double[] dBunch() {
		double[] l = new double[ntokens-itoken];
		int i = 0;
		while (itoken < ntokens) l[i++] = Double.parseDouble(tokens[itoken++]);
		return l;
	}

	
	// -- utils
	
	// keep leading zeroes...
	public int[] breakdownNumber(String s) {
		int n = (s == null ? 0 : s.length());
		int[] aa = new int[n];
		if (n > 0) {
			char[] cc = s.toCharArray();
			for (int i=0 ; i<n ; i++) {
				aa[i] = cc[i] - '0';
			}
		}
		return aa;
	}
	
	
	// --
	
	public void terminate() {
		try {
			if (br != null) br.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("");
	}
	
	
	// --- OUTPUT ---
	
	public static void outcase(int n, boolean rc) {
		System.out.print("Case #" + n + ": ");
		if (rc) System.out.print("\n");
	}
	
}
