import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;


public class GoogleC {
	public String szamol(String input){
		String result="";
		String a=input;
		Integer min=Integer.parseInt(a.substring(0,a.indexOf(' ')));
		a=a.substring(a.indexOf(' ')+1);
		Integer max=Integer.parseInt(a);
		Long count=0l;
		for(Integer i=min; i<=max; i++){
			String e1=i.toString();
			Set<Integer> db=new HashSet<Integer>(); 
			for (int j=1; j<e1.length();j++){
				
				String e2=e1.substring(j)+e1.substring(0,j);
				Integer k=Integer.parseInt(e2);
				if (k>i && k<=max){
					db.add(k);
					//System.out.println(e1+"\t"+e2);
				}
				
			}
			count+=db.size();
		}
		
		return count.toString();
	}
	public static void main(String[] args) {
		GoogleC a=new GoogleC();
		//String filename="input.txt";
		//String filename="C-small-attempt0.in";
		String filename="C-large.in";
		String thisLine;
		try {
			BufferedReader br = new BufferedReader(new FileReader(filename));
			BufferedWriter bw= new BufferedWriter(new FileWriter(filename+".out"));
			thisLine=br.readLine();
			Integer tnum=Integer.parseInt(thisLine);
		     for(int i=0;i<tnum;i++) { // while loop begins here
		    	 	thisLine=br.readLine();
		           System.out.println(thisLine);
		           System.out.println("Case #"+(i+1)+": "+a.szamol(thisLine)+"\n");
		           bw.write("Case #"+(i+1)+": "+a.szamol(thisLine)+"\n");
		         } // end while 
		        // end try
		     br.close();
		     bw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

}

import java.io.*;
import java.util.*;

public class QualiC_Naive
{
  static void sop(String str)
  {
    System.out.println(str);
  }
  
  static void sop1(String str)
  {
    System.out.print(str);
  }
  
  public static void main(String args[])
  {
    long tstart = System.currentTimeMillis();
    if(args.length < 1)
    {
      System.out.println("You have not given input file!");
      System.exit(0);
    }
    File f1 = null;
    Scanner sc = null;
    try
    {
      f1 = new File(args[0]);
      sc = new Scanner(f1);
    }
    catch(Exception e)
    {
      System.out.println("Error opening the input file " + e);
    }
    /*
      int nCases, i, j, k, m, n;
      sc.next();
      sc.nextInt();
      sc.nextLine();
      sc.nextDouble();
    */
    int nCases, i, j, k, m, n;
    nCases = sc.nextInt();
    // sc.nextLine();
    for(i=1; i<=nCases; ++i)
    {
      int a = sc.nextInt();
      int b = sc.nextInt();
      int ans = 0;
      String strn = "" + a;
      // String strm = "" + b;
      int len = strn.length();
      int mult=0;
      switch(len)
      {
        case 2: mult = 10; break;
        case 3: mult = 100; break;
        case 4: mult = 1000; break;
        case 5: mult = 10000; break;
        case 6: mult = 100000; break;
        case 7: mult = 1000000; break;
      }
      if(len == 1)
        ans = 0;
      else
      { 
      /*
        try
	{
	  FileWriter tempf = new FileWriter("temp.dat");
	  BufferedWriter buff = new BufferedWriter(tempf);
	  buff.write("Case " + i + ":-------------------------------------------\n");
      */
        int c[] = new int[len];
        for(j=a; j<=b; j++)
	{ 
	  c[0] = j;
	  HashSet<Integer> x = new HashSet<Integer>();
	  for(k=1; k<len; k++)
	  {
	    c[k] = (c[k-1] % 10) * mult + c[k-1] / 10;
            //if(j<c[k] && c[k]<=b)
            if(j<c[k])
	      if(c[k]<=b)
	      {
	        if(x.add(c[k]))
	          ans++;
	   //   buff.write("" + j + " - " + c2 + "\n");
	      }
	  }

	}
	/*
	  buff.close();
	}
	catch(IOException e1)
	{
	  sop("Error: " + e1);
	}
	*/
      }
  
      sop("Case #" + i + ": " + ans);
    }
    long tend = System.currentTimeMillis();
    System.err.println("Time taken: " + (tend - tstart)/1000.0 + " seconds");
  } // end of main()
}

