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 Q3{
  public static void main(String args[]){
    try {
      BufferedReader is = new BufferedReader(
        new InputStreamReader(new FileInputStream(new File("C-large.in"))));
      BufferedWriter os = new BufferedWriter(
        new OutputStreamWriter(new FileOutputStream(new File("q3output-l.txt"))));

      int num = Integer.parseInt(in(is));

      for (int i=0;i<num;i++){
        String s = in(is);

        
        String o = answer(s);


        p("Case #" + (i + 1) + ": " + o, os);

      }
      is.close();
      os.close();
    }catch(Exception e){
      e.printStackTrace();
    }
  }

  private static String in(BufferedReader is) throws IOException{
    return is.readLine();
  }

  private static void p(String n, String w){
    System.out.println(n + ":" + w);
  }

  private static void p(String w) {
    System.out.println(w);
  }

  private static void p(int w) {
    p(""+w);
  }

  private static void p(String w, BufferedWriter os) throws IOException{
    os.write(w);
    os.newLine();
  }

  private static String answer(String s) {
    StringTokenizer sb = new StringTokenizer(s);
    int result = 0;
    int min = Integer.parseInt(sb.nextToken());
    int max = Integer.parseInt(sb.nextToken());
    int border = first(max);
    
    for (int i=min;i<=max;i++){
    	result += pair(i, border, max);
    }
    return ""+result;
  }

  private static int pair(int i, int border, int max){
    int result = 0;
    int first = first(i);
    int num = i;
    int target = 0;
    int out = 0;
    Set<Integer> dup = new HashSet<Integer>();
    while(num >= 10 ) {
      target = num % 10;
      num = num/10;
      out++;
      int convert = convert(num, i, out);
      if (target > first && target < border) {
        if(!dup.contains(convert)){
          dup.add(convert);
          result++;
        }
      } else if(target == first && target == border) {
        if(!dup.contains(convert) && compare(convert, i) && !compare(convert, max)){
          dup.add(convert);
          result++;
        }
      } else if(target == first) {
        if(!dup.contains(convert) && compare(convert, i) && !compare(convert, max)){
          dup.add(convert);
          result++;
        }
      } else if(target == border) {
        if(!dup.contains(convert) && compare(convert, i) && !compare(convert, max)){
          dup.add(convert);
          result++;
        }
      }
    }
    return result;
  }

  private static int first(int num){
    int ret = num;
    while(ret >= 10) {
      ret = ret/10;
    }
    return ret;
  }

  private static int convert(int num, int all, int out){
    int m = power(out);
    int p = digits(num);
    return (all - num * m) * p + num;
  }

  private static boolean compare(int n, int com){
    return n > com;
  }

  private static int digits(int num){
    int ret = 10;
    while(num >= 10) {
      num = num/10;
      ret*=10;
    }
    return ret;
  }

  private static int power(int out){
    int ret = 1;
    for(int i =0;i<out;i++){
      ret *= 10;
    }
    return ret;
  }
}

