import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Q3M {
	public static void main(String[] args) throws Exception {
		compute();
	}

	private static void compute() throws Exception {
		BufferedReader br = new BufferedReader(new FileReader(new File(
				"C:\\work\\Q3\\C-small-attempt0.in")));
		String line = null;
		int i = Integer.parseInt(br.readLine());
		List l = new ArrayList();
		for (int j = 0; j < i; j++) {
			line = br.readLine();
			String[] nums = line.split(" ");
			l.add(calculate(nums));
		}
		writeOutput(l);
	}

	private static int calculate(String[] nums) {
		int min = Integer.parseInt(nums[0]);
		int max = Integer.parseInt(nums[1]);
		int count = 0;
		List l = new ArrayList();
		for (int i = min; i <= max; i++) {
			for (int times = 1; times < countDigits(i); times++) {
				int res = shiftNum(i, times);
				if (res <= max && i < res) {
					if ((!l.contains((i + ":" + res)))) {
						l.add(i + ":" + res);
						l.add(res + ":" + i);
						count++;
					}
				}
			}
		}
		return count;
	}

	private static boolean checkZeros(int temp, int res) {
		if (temp % 10 == 0 || res % 10 == 0)
			return false;
		return true;
	}

	private static int shiftNum(int n, int times) {
		int base = (int) Math.pow(10, times);
		int temp2 = n / base;
		int placeHolder = (int) Math.pow((double) 10,
				(double) countDigits(temp2));
		int res = placeHolder * (n % base) + temp2;
		if (countDigits(res) == countDigits(n)) {
			return res;
		} else {
			return 2000001;
		}
	}

	public static int countDigits(int x) {
		if (x < 10)
			return 1;
		else {
			return 1 + countDigits(x / 10);
		}
	}

	private static void writeOutput(List l) throws Exception {
		StringBuffer b = new StringBuffer();
		int i = 1;
		BufferedWriter br = new BufferedWriter(new FileWriter(new File(
				"C:\\work\\Q3\\ans.txt")));
		for (Iterator iterator = l.iterator(); iterator.hasNext();) {
			br.write("Case #" + i++ + ": " + iterator.next());
			br.newLine();
		}
		br.close();
	}

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