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


	}

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codejam;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Ka
 */
public class Recy {
        private File file;
    private int aantal;
    private ArrayList<String> cases;
    private ArrayList<Integer> max;
    public Recy(File f) throws FileNotFoundException
    {
        file = f;
        Scanner scanner = new Scanner(new FileReader(file));
        aantal = scanner.nextInt();
        cases = new ArrayList<String>(aantal);
        max = new ArrayList<Integer>(aantal);
        scanner.nextLine();
        for(int i =0; i < aantal ; i++)
        {
            String s = scanner.nextLine();
            cases.add(s);
        }
        System.out.println(cases);
    }
    public void scores()
    {
        for(String s : cases)
        {
            int counter = 0;
            ArrayList<Integer> nb = new ArrayList<Integer>();
            String sb = "";
            while(counter < s.length())
            {
                if(s.charAt(counter)!= ' ')
                {
                    sb = sb + s.charAt(counter);
                }
                if(s.charAt(counter) == ' '&& counter != s.length()-1)
                {
                    int n = Integer.parseInt(sb);
                    nb.add(n);
                    sb = "";
                }
                counter++;
            }
            if(counter == s.length())
            {
                int n = Integer.parseInt(sb);
                nb.add(n);
                sb = "";
            }
            System.out.println(nb);
            options(nb);
        }
    }
    public void options(ArrayList<Integer>nr)
    {
        int n = nr.get(0), m = nr.get(1),amount=0;
        if(n < 10 && m < 10)
            amount = 0;
        else if(n >= 10 && n < 100&& n % 10 < 10)
        {
            for(int i = n ; i < m ; i++)
            {
                String s = Integer.toString(i);
                int g = Integer.parseInt(s);
                String s1 = s.charAt(1)+""+ s.charAt(0)+"";
                int r = Integer.parseInt(s1);
                if(g >= n && g <= m && r >= n && r <= m && g < r )
                    amount++;
            }
        }
        else if(n >= 100 && n < 1000)
        {
            for(int i = n ; i < m ; i++)
            {
                String s = Integer.toString(i);
                int g = Integer.parseInt(s);
                String s1 = s.charAt(2)+""+ s.charAt(0)+""+s.charAt(1);
                String s2 = s.charAt(1)+""+ s.charAt(2)+""+s.charAt(0);
                int r = Integer.parseInt(s1);
                int r1 = Integer.parseInt(s2);
                if(g >= n && g <= m && r >= n && r <= m && g < r )
                    amount++;
                if(r != r1)
                {
                    if(g >= n && g <= m && r1 >= n && r1 <= m && g < r1 )
                        amount++;
                }
            }
        }
        else if(n >= 1000 && n < 10000)
        {
            for(int i = n ; i < m ; i++)
            {
                String s = Integer.toString(i);
                int g = Integer.parseInt(s);
                String s1 = s.charAt(3)+""+s.charAt(0)+""+ s.charAt(1)+""+s.charAt(2);
                String s2 = s.charAt(2)+""+s.charAt(3)+""+ s.charAt(0)+""+s.charAt(1);
                String s3 = s.charAt(1)+""+s.charAt(2)+""+ s.charAt(3)+""+s.charAt(0);
                int r = Integer.parseInt(s1);
                int r1 = Integer.parseInt(s2);
                int r2 = Integer.parseInt(s3);
                if(g >= n && g <= m && r >= n && r <= m && g < r )
                    amount++;
                if(r != r1 && r1 != r2 && r != r2)
                {
                    if(g >= n && g <= m && r1 >= n && r1 <= m && g < r1 )
                        amount++;
                    if(g >= n && g <= m && r2 >= n && r2 <= m && g <= r2 )
                        amount++;
                }
            }
        }
        else if(n >= 10000 && n < 100000)
        {
            for(int i = n ; i < m ; i++)
            {
                String s = Integer.toString(i);
                int g = Integer.parseInt(s);
                String s1 = s.charAt(4)+""+s.charAt(0)+""+s.charAt(1)+""+ s.charAt(2)+""+s.charAt(3);
                String s2 = s.charAt(3)+""+s.charAt(4)+""+s.charAt(0)+""+ s.charAt(1)+""+s.charAt(2);
                String s3 = s.charAt(2)+""+s.charAt(3)+""+s.charAt(4)+""+ s.charAt(0)+""+s.charAt(1);
                String s4 = s.charAt(1)+""+s.charAt(2)+""+s.charAt(3)+""+ s.charAt(4)+""+s.charAt(0);
                int r = Integer.parseInt(s1);
                int r1 = Integer.parseInt(s2);
                int r2 = Integer.parseInt(s3);
                int r3 = Integer.parseInt(s4);
                if(g >= n && g <= m && r >= n && r <= m && g < r )
                    amount++;
                if(r != r1 || r1 != r2 || r != r2 || r != r3 || r2 != r3)
                {
                    if(g >= n && g <= m && r1 >= n && r1 <= m && g < r1 )
                        amount++;
                    if(g >= n && g <= m && r2 >= n && r2 <= m && g <= r2 )
                        amount++;
                    if(g >= n && g <= m && r3 >= n && r3 <= m && g <= r3 )
                        amount++;
                }
            }
        }
        else if(n >= 1000000 && n < 10000000)
        {
            for(int i = n ; i < m ; i++)
            {
                String s = Integer.toString(i);
                int g = Integer.parseInt(s);
                String s1 = s.charAt(5)+""+s.charAt(0)+""+s.charAt(1)+""+s.charAt(2)+""+ s.charAt(3)+""+s.charAt(4);
                String s2 = s.charAt(4)+""+s.charAt(5)+""+s.charAt(0)+""+s.charAt(1)+""+ s.charAt(2)+""+s.charAt(3);
                String s3 = s.charAt(3)+""+s.charAt(4)+""+s.charAt(5)+""+s.charAt(0)+""+ s.charAt(1)+""+s.charAt(2);
                String s4 = s.charAt(2)+""+s.charAt(3)+""+s.charAt(4)+""+s.charAt(5)+""+ s.charAt(0)+""+s.charAt(1);
                String s5 = s.charAt(1)+""+s.charAt(2)+""+s.charAt(3)+""+s.charAt(4)+""+ s.charAt(5)+""+s.charAt(0);
                int r = Integer.parseInt(s1);
                int r1 = Integer.parseInt(s2);
                int r2 = Integer.parseInt(s3);
                int r3 = Integer.parseInt(s4);
                int r4 = Integer.parseInt(s5);
                if(g >= n && g <= m && r >= n && r <= m && g < r )
                    amount++;
                if(r != r1 || r1 != r2 || r != r2 || r != r3 || r2 != r3 || r != r4 || r2 != r4 || r3 != r4)
                {
                    if(g >= n && g <= m && r1 >= n && r1 <= m && g < r1 )
                        amount++;
                    if(g >= n && g <= m && r2 >= n && r2 <= m && g <= r2 )
                        amount++;
                    if(g >= n && g <= m && r3 >= n && r3 <= m && g <= r3 )
                        amount++;
                    if(g >= n && g <= m && r4 >= n && r4 <= m && g <= r4 )
                        amount++;
                }
            }
        }
        max.add(amount);
    }
        public void output(BufferedWriter out) throws FileNotFoundException, IOException
    {
        int counter = 1;
        for(Integer s : max )
        {
            out.write("Case #" + counter + ": ");
            out.write(s+"\n");
            counter++;
        }
        out.close();
    }
}
