/*
 * 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.*;
/**
 * Write a description of class Googlerese here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class RecycledNumbers
{
    public static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    
    private int x;
    private int max;
    public RecycledNumbers(int n)
    {
        x=0;
        max=n;
    }
    
        public int countdigit(int b)
        {
            int ctr=0;
            while(b>0)
            {
                b/=10;
                ctr++;
            }
            return ctr;
        }
        
        public int fact(int a)
        {
            int fact=1;
            int b=countdigit(a)-1;
                while(b>0)
                {
                    fact*=10;
                    b--;
                }
                return fact;
        }
                    
   public void input(int n,int m)
   {
       int success=0;
       for(int i=n;i<=m;i++)
       {
           for(int j=n;j<=m;j++)
           {
               if(j>i)
               {
                    if(calculate(i,j))
                        success++;
                }
           }
       }
        //System.out.println("Success="+success);
        
        System.out.print("Case #"+(x+1)+": ");
        System.out.println(success);
        x+=1;
        if(x==max)
            return;
       
    }
   
 
            
   public boolean calculate(int n,int m )
   {
     
     int cpy=countdigit(n);
     int aux=0;
     int success=0;
     
     int test=n;
    while(aux<cpy)
     //while(rd<cpy)
     {
         int x=fact(n);
         
         int y=n/x;
         n=n%(y*x);
         //System.out.println(n);
         n=n*10;
         n=n+y;
         while(cpy>countdigit(n))
            n*=10;
         
         //System.out.println(n);
         if(m==n)
         {
             //System.out.println(test+"="+m);
             //success++;
             return true;
         }
         aux++;
         
         
     }
         
     return false;
     
   }
    
    
    
    
    
    public void start()throws IOException
    {
        // put your code here
       int i=1; 
        File file = new File("C-small-attempt.txt");
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
      fis = new FileInputStream(file);

      // Here BufferedInputStream is added for fast reading.
      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);

      // dis.available() returns 0 if the file does not have more lines.
      while (dis.available() != 0) {

          if(x==max)
            return;
      // this statement reads the line from the file and print it to
        // the console.
       // System.out.println(dis.readLine());
       String s=dis.readLine();
       StringTokenizer st=new StringTokenizer(s," ");
       while(st.hasMoreElements())
       {
          
            int x=Integer.parseInt(st.nextElement().toString());
           //System.out.println(x);
           
           int y=Integer.parseInt(st.nextElement().toString());
           //System.out.println(y);
           input(x,y);
           
      }
    }

      // dispose all the resources after using them.
      fis.close();
      bis.close();
      dis.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  //Read File Line By Line
  
  //Close the input stream
  
  }
   
     
        
    public static void main(String[]args)throws IOException     
    {
        
        int n;
        do{
         n=Integer.parseInt(br.readLine());
        }while(n<1||n>50);
        RecycledNumbers obj=new RecycledNumbers(n);
        for(int i=0;i<n;i++)
        {
            
        obj.start();
        //System.out.println("Output");
        
    }}}
