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.math.*;
import java.io.*;
import java.util.*;

public class Recycle {

    public static void main(String[] args) {
	Recycle r = new Recycle();
	writeOutput(r.countRecycleds(readInput()));
    }

    // Count all recycleds for all the inputs
    public String countRecycleds(ArrayList<String> lines) {
	String line;
	StringBuffer out = new StringBuffer();
	for (int i = 1; i < lines.size(); i++) {
	    line = lines.get(i);
	    String[] args = line.split(" ");
	    out.append("Case #" + i + ": ");
	    out.append(countRecycleds(Integer.parseInt(args[0]),
				      Integer.parseInt(args[1])));
	    out.append("\n");
	}
	return out.toString();
    }

    // Count all recycleds between A and B
    public int countRecycleds(int A, int B) {
	int count = 0;
	for (int n = A; n <= B; n++) {
	    count += countRecycledsForN(n, B);
	}
	return count;
    }

    // Count recycleds for one n (ie, number of ms)
    public int countRecycledsForN(int n, int B) {
	int digits = countDigits(n);
	int count = 0;
	Map<Integer,Boolean> used = new HashMap<Integer, Boolean>();
	// For each number of digits we're swapping
	for(int swap = 1; swap < digits; swap++) {
	    // The base 10 number to swap
	    int swapper = (int)Math.pow(10, swap);
	    // The digits being swapped
	    int swappedDigits = n % swapper;
	    // n without the digits being swapped
	    int swappedN = n / swapper;
	    int m = (swappedDigits * (int)Math.pow(10, digits - swap)) 
		+ swappedN;
	    // If m is valid, count it
	    if (n < m && m <= B && !used.containsKey(m)) {
		count++;
		used.put(m, true);
	    }
	}
	return count;
    }
    
    // Count the number of digits in n (base 10)
    public int countDigits(int n) {
	return (int)Math.log10(n) + 1;
    }

    public static void writeOutput(String s) {
	try{
	    BufferedWriter out = new BufferedWriter(new FileWriter("./Output.txt"));
	    out.write(s);
	    out.close();
	}catch (Exception e){
	    System.out.println(s);
	    throw new RuntimeException("Oops, couldn't write");
	}
    }

    public static ArrayList<String> readInput() {
	ArrayList<String> lines = new ArrayList<String>();
	try {
	    FileInputStream fstream = new FileInputStream("./Input.txt");
	    DataInputStream in = new DataInputStream(fstream);
	    BufferedReader br = new BufferedReader(new InputStreamReader(in));
	    String str;
	    while ((str = br.readLine()) != null) {
		lines.add(str);
	    }
	    br.close();
	    return lines;
	} catch (Exception e) {
	    throw new RuntimeException("Couldn't read");
	}
    }
}