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


import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
class CodeJamTools

// http://snippets.dzone.com/posts/show/1335
{
	public static String readFileAsString(String filePath) {
	    byte[] buffer = new byte[(int) new File(filePath).length()];
	    BufferedInputStream f = null;
	    try {
	        f = new BufferedInputStream(new FileInputStream(filePath));
	        f.read(buffer);
	        f.close();
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
	    return new String(buffer);
	}


	//http://www.daniweb.com/software-development/java/threads/94567
	public static void writeStringToFile(String filename, String output) {

		PrintStream fout;
		try {
			fout = new PrintStream(new FileOutputStream (filename));
			fout.print(output);
			fout.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static int[] readIntsFromRow(String row) {
		String[] cText = row.split("\\s");
		int[] result = new int[cText.length];
		for (int i=0; i<cText.length; i++) {
			result[i] = new Integer(cText[i]);
		}
		return result;
	}

	public static long[] readLongsFromRow(String row) {
		String[] cText = row.split("\\s");
		long[] result = new long[cText.length];
		for (int i=0; i<cText.length; i++) {
			result[i] = new Long(cText[i]);
		}
		return result;
	}

	public static Set<Long> readLongsSetFromRow(String row) {
		String[] cText = row.split("\\s");
		Set<Long> result = new TreeSet<Long>();
		for (int i=0; i<cText.length; i++) {
			result.add(new Long(cText[i]));
		}
		return result;
	}



	public static double[] readDoublesFromRow(String row) {
		String[] cText = row.split("\\s");
		double[] result = new double[cText.length];
		for (int i=0; i<cText.length; i++) {
			result[i] = new Double(cText[i]);
		}
		return result;
	}

	public static String pilkuta(String t) {
		String res = "[";
		for (int i=0; i<t.length(); i++) {
			if (i>0)
				res += ", ";
			res += t.substring(i,i+1);
		}
		res += "]";
		return res;
	}


	// http://www.proglogic.com/code/java/calculator/lcm.php

	public static long leastCommonMultiplier (long m, long n){
		return m * (n / greatestCommonDivisor(m, n));
	}

	public static long greatestCommonDivisor (long m, long n){
		long x;
		long y;
		while(m%n != 0){
			x = n;
			y = m%n;
			m = x;
			n = y;
		}
		return n;
	}

	//http://www.vogella.de/articles/JavaAlgorithmsPrimeFactorization/article.html:

	public static List<Integer> primeFactors(int number) {
		int n = number;
		List<Integer> factors = new ArrayList<Integer>();
		for (int i = 2; i <= n; i++) {
			while (n % i == 0) {
				factors.add(i);
				n /= i;
			}
		}
		return factors;
	}
}
	