/*
 * 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.util.*;

public class RecycledNum
{
  private static int testCase;

  
  public static void main( String[] args )
  {
    Scanner s = new Scanner( System.in );
    RecycledNum r = new RecycledNum();
    r.setTestCase(Integer.parseInt(s.nextLine()));
    for( int i = 0; i < testCase; i++ )
    {
      int A = s.nextInt();
      int B = s.nextInt();
      s.nextLine();
      if( B/A < 10)
      {
        System.out.println( "Case #" +(i+1) +": " + r.recycle( A, B ) );
      }
    }
  }
  public RecycledNum()
  {
    this.testCase = 1;
  }
  public void setTestCase( int T )
  {
    if( T>=1 && T<=50 )
      this.testCase = T;
  }
  public int getTestCase()
  {
    return this.testCase;
  }
  public int recycle(int A, int B)
  {
    int n = 0;
    double j = A;

    int count = 0;
    while( j >= 1)
    {
      j = j/10;
      n = n + 1;
    }
    for( int i = A; i <= B; i++ )
    {
      //System.out.println("original "+ i);
      char[] array = new char[n];
      String numstri = i + "";
      for( int m = 0; m < n; m++)
      {
        array[m]=numstri.charAt(m);
      }
      int[] pair = new int[n];
      for( int q = 0; q< pair.length; q++)
      {
        pair[q] = 0; 
      }
      int count2 = 0;
      for ( int k = 0 ; k < n-1; k++ )
      {
        
        String str = "";
        String rts = "";
        for( int p = 0; p < n - k-1; p++ )
        {
          str = str + array[p];
          ///System.out.println( str );
        }
        for( int p = n-k-1; p < n; p++)
        {
          rts = rts + array[p];
           //System.out.println(rts );
        }
        String stri = rts + str;
        // System.out.println( stri);
        int num = Integer.parseInt(stri);
        boolean check = true;
        if( num <= B && num >= A && num != i )
        {
          
          for( int q = 0; q < pair.length;q++ )
          {
            if(pair[q] == num ){
              check = false;
            }
          }
          if( check )
          {
            count++;
            count2++;
           // System.out.println(count);
            pair[count2-1] = num;
            //System.out.println( num +" ");
          }
          
        }
      }
 
    }
    return count/2;
  }
  
  
}