
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;

public class Recycle
{
	public static void main(String[] args)
	{
/*		Set<Integer> perms = getPermutations(123456);
		
		for(Integer i: perms)
			System.out.println(i);*/
	
		try
		{
			Scanner s = new Scanner(new File("recycle.in"));
			BufferedWriter w = new BufferedWriter(new FileWriter(new File("recycle.out")));
			int numCases = s.nextInt();
			s.nextLine();
			
			for(int n = 1;n <= numCases; n++)
			{
				int A = s.nextInt();
				int B = s.nextInt();
				int count = 0;
				for(int k = A; k <= B; k++)
				{
					count += getNumPairs(k, B);
				}
				
				w.write("Case #" + n + ": " + count + "\n");
			}
			
			w.flush();
			w.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public static int getNumPairs(int n, int B)
	{
		Set<Integer> possibles = getPermutations(n);
		int count = 0;
		for(Integer i : possibles)
			if(i > n && i <= B)
				count++;
		return count;
	}
	
	public static Set<Integer> getPermutations(int n)
	{
		Set<Integer> perms = new TreeSet<Integer>();
		char[] digits = String.valueOf(n).toCharArray();
		
		for(int firstPos = 1; firstPos < digits.length; firstPos++)
		{
			String toBuild = "";
			for(int k = 0; k < digits.length; k++)
				toBuild += digits[(firstPos + k) % digits.length];
			perms.add(Integer.parseInt(toBuild));
		}
		return perms;
	}
	

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