import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

class RecycledNumbers{
	
	
	
	int getcount(int small,int large){
		int count = 0;
		for(int currnum = small; currnum < large; currnum++){
			int permut = currnum;
			int no_of_digits = 0;
			while(permut > 0){
				permut = permut/10;
				no_of_digits++;
			}
			int lastDigit = currnum % 10;
			permut = currnum / 10;
			permut = permut + lastDigit * (int)Math.pow(10, no_of_digits-1);
			while(permut != currnum){
				
				if(permut >= currnum && permut <= large){
					
					count++;
				}
				lastDigit = permut % 10;
				permut = permut / 10;
				permut = permut + lastDigit * (int)Math.pow(10, no_of_digits-1);
				
			}
			
		}
		
		return count;
	}
	
	public static void main(String args[]){
		RecycledNumbers d = new RecycledNumbers();
		
		int no_of_cases = 0;
		String input = "";
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try{
			no_of_cases = Integer.parseInt(br.readLine());
		}
		catch(Exception e){
			System.out.println("First line is not a number");
		}
		
		for(int i=1; i <= no_of_cases;i++){
			try{
				input = br.readLine();
			}
			catch(Exception e){
				System.out.println("Number of test cases different from the number mentioned in first line");
			}
			
			System.out.print("Case #" +i+": ");
			
			StringTokenizer t =  new StringTokenizer(input);
			int small=0,large=0;
			if(t.hasMoreTokens()){
				small = Integer.parseInt(t.nextToken());
			}
			if(t.hasMoreTokens()){
				large = Integer.parseInt(t.nextToken());
			}
			System.out.println(d.getcount(small,large));
			
		}
		
		
		

	}
	
	
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;

public class QualCRecycledNumbers
{
   static boolean debug = false;
   static int MIN = 1, MAX = 2500;
   //static int MIN = 1, MAX = 2000000;

   static void print(String str) { if (debug) System.out.print(str); }
   static void println(String str) { if (debug) System.out.println(str); }
   static void println(int str) { if (debug) System.out.println(str); }
   static void println() { if (debug) System.out.println(); }

   public static void main(String[] args)
   {
      Utils.openInFile("C-small-attempt0.in");
      Utils.openOutFile("out.txt", debug);
      String input = Utils.readFromFile();
      int lineCount = Integer.parseInt(input);

      HashMap<Integer, ArrayList<Integer>> recyclable = new HashMap<Integer, ArrayList<Integer>>();
      HashSet<Integer> cleared = new HashSet<Integer>(MAX);

      for (int i = MIN; i < MAX; i++)
      {
         if (i % 100000 == 0)
            System.out.println(i);
         if (cleared.contains(i))
            continue;
         int newVal = i;
         String iStr = ""+i;
         ArrayList<Integer> array = new ArrayList<Integer>();
         print("i="+i+": ");
         do
         {
            String numStr = ""+newVal;
            while (numStr.length() < iStr.length())
               numStr = "0"+numStr;
            numStr = numStr.charAt(numStr.length()-1) + numStr.substring(0, numStr.length()-1);
            newVal = Integer.parseInt(numStr);
            if ((""+newVal).length() < iStr.length())
               continue;
            cleared.add(newVal);
            print(newVal + " ");
            array.add(newVal); // adds itself at the end
         }
         while (newVal != i);
         println();
         if (!array.isEmpty())
         {
            Collections.sort(array);
            recyclable.put(i, array);
         }
      }

      for (int line = 1; line <= lineCount; line++)
      {
         println();
         input = Utils.readFromFile();
         String[] split = input.split(" ");
         int AMin = Integer.parseInt(split[0]),
             BMax = Integer.parseInt(split[1]);

         ArrayList<Integer> array;
         int count = 0;
         for (int i = 0; i <= BMax; i++)
         {
            if ((array = recyclable.get(i)) != null)
            {
               // min is implicitly enforced from structure
               int tempcount = 0;
               int stop = array.size(),
                   start = array.size();
               for (int k = 0; k < array.size(); k++)
               {
                  if (start == array.size() && array.get(k) >= AMin)
                     start = k;
                  if(array.get(k) > BMax)
                     { stop = k; break; }
               }
               if (stop == 1) continue;
               int diff = stop-start;
               //tempcount += stop*(stop-1)/2;
               tempcount += diff*(diff-1)/2;
               count += tempcount;
               print("i="+i+" count="+tempcount+", diff="+(stop-start));
               for (int j = start; j < stop; j++)
               {
                  print(" "+array.get(j));
               }
               println();
            }
         }
         println("input="+input);
         String output = "Case #" + line + ": ";
         output += count;
         Utils.writeToFile(output);
         //if (line == 3) break;
      }
      Utils.closeInFile();
      Utils.closeOutFile();
   }
}
