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



/**
 * Write a description of class solver here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class solver
{
    public int aise(int a[],int k,int j)
    {
        int i;
        int temp=0;
        for(i=0;i<j;i++)
        {
            temp=temp*10+a[(i+k)%j];
        }
        return temp;
    }

    public int[] reverse(int a[],int j)
    {
        int i,temp;
        for(i=0;i<j/2;i++)
        {
            temp=a[i];
            a[i]=a[j-i-1];
            a[j-i-1]=temp;
        }
        return a;
    }

  public boolean greater(int a[],int i,int n)
  {
      int j=0;
      for(j=0;j<n;j++)
      {
          if(a[(i+j)%n]<a[j])
          return false;
          else if(a[(i+j)%n]>a[j])
          return true;
        }
        return false;
    }
    public boolean lesser(int a[],int b[],int k,int j,int n1)
    {
        if(j<n1)
        return true;
        else
        {
            int i=0;
            for(i=0;i<n1;i++)
            {
                if(a[(k+i)%j]>b[i])
                return false;
                else if(a[(k+i)%j]<b[i])
                return true;
            }
            return true;
        }
    }


    public int solve(int a,int b)
  {
      int i,k,s;
      int n1=0,temp,temp1=b,j;
      int[] arr=new int[10];
      int count=0;
      int[] arr2=new int[10];
      int[] comp=new int[10];
      while(temp1>0)
          {
              arr2[n1++]=temp1%10;
              temp1=temp1/10;
           }
    
      arr2=reverse(arr2,n1);
      for(i=a;i<b;i++)
      { 
          temp=i;
          j=0;
          while(temp>0)
          {
              arr[j++]=temp%10;
              temp=temp/10;
           }
           arr=reverse(arr,j);
           s=0;
           for(k=j-1;k>0;k--)
           {
               if(arr[k]!=0)
               {
                   if(greater(arr,k,j) && lesser(arr,arr2,k,j,n1))
                   {
                   count++;
                   comp[s++]=aise(arr,k,j);
                }
                }
            }
            for(k=0;k<s;k++)
            {
                for(j=k+1;j<s;j++)
                    {
                           if(comp[k]==comp[j])
                           {
                               count--;
                            }
                        }
                    }
                
            }
          return count;
        }
      
      
    }
    

