package jp.funnything.competition.util;

import java.math.BigInteger;

/**
 * Utility for BigInteger
 */
public class BI {
    public static BigInteger ZERO = BigInteger.ZERO;
    public static BigInteger ONE = BigInteger.ONE;

    public static BigInteger add( final BigInteger x , final BigInteger y ) {
        return x.add( y );
    }

    public static BigInteger add( final BigInteger x , final long y ) {
        return add( x , v( y ) );
    }

    public static BigInteger add( final long x , final BigInteger y ) {
        return add( v( x ) , y );
    }

    public static int cmp( final BigInteger x , final BigInteger y ) {
        return x.compareTo( y );
    }

    public static int cmp( final BigInteger x , final long y ) {
        return cmp( x , v( y ) );
    }

    public static int cmp( final long x , final BigInteger y ) {
        return cmp( v( x ) , y );
    }

    public static BigInteger div( final BigInteger x , final BigInteger y ) {
        return x.divide( y );
    }

    public static BigInteger div( final BigInteger x , final long y ) {
        return div( x , v( y ) );
    }

    public static BigInteger div( final long x , final BigInteger y ) {
        return div( v( x ) , y );
    }

    public static BigInteger mod( final BigInteger x , final BigInteger y ) {
        return x.mod( y );
    }

    public static BigInteger mod( final BigInteger x , final long y ) {
        return mod( x , v( y ) );
    }

    public static BigInteger mod( final long x , final BigInteger y ) {
        return mod( v( x ) , y );
    }

    public static BigInteger mul( final BigInteger x , final BigInteger y ) {
        return x.multiply( y );
    }

    public static BigInteger mul( final BigInteger x , final long y ) {
        return mul( x , v( y ) );
    }

    public static BigInteger mul( final long x , final BigInteger y ) {
        return mul( v( x ) , y );
    }

    public static BigInteger sub( final BigInteger x , final BigInteger y ) {
        return x.subtract( y );
    }

    public static BigInteger sub( final BigInteger x , final long y ) {
        return sub( x , v( y ) );
    }

    public static BigInteger sub( final long x , final BigInteger y ) {
        return sub( v( x ) , y );
    }

    public static BigInteger v( final long value ) {
        return BigInteger.valueOf( value );
    }
}

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