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

public class RecycledNumbers {
	BufferedReader reader;
	char [] charInput;
	String [] inputArray;
	int index;
	RecycledNumbers()throws Exception{
		String input=null;
		reader = new BufferedReader(new InputStreamReader(System.in));
		while(reader.ready()){
			if (input==null){
				input =reader.readLine();
			}
			else{
				input =input+ " " + reader.readLine();
			}
		}
		inputArray=input.trim().split(" ");
		index=0;
	}
	String nextInput()throws Exception{
		if(index<inputArray.length){
			int temp=index;
			index++;
			return inputArray[temp];
		}
		else{
			return "\0";
		}
	}
	
	
	void start()throws Exception{
		LinkedList<Integer> prevMatches=new LinkedList<Integer>();
		int numCases=Integer.parseInt(nextInput());
		int A;
		int B;
		int numRecycledPairs=0;
		int [] numArray;
		int digits;
		int testNum=0;
		boolean prevExists=false;
		for(int i=0;i<numCases;i++){
			A=Integer.parseInt(nextInput());
			B=Integer.parseInt(nextInput());
			digits=numDigits(A);
			numRecycledPairs=0;
			for(int j=A;j<=B;j++){
				prevMatches.clear();
				numArray=new int[digits*2-1];
				testNum=j;
				for (int k=numArray.length-1;k>=0;k--){
					testNum=testNum/10;
					if(k==digits-1){
						testNum=j;
					}
					numArray[k]=testNum%10;
				}
				for (int k=1;k<digits;k++){
					testNum=0;
					for (int l=k;l<digits+k;l++){
						testNum=testNum*10;
						testNum=testNum+numArray[l];
					}
					if (prevMatches.size()==0){
						prevExists=false;
					}
					else{
						for (Integer m: prevMatches){
							if (m.intValue()==testNum){
								prevExists=true;
								break;
							}
							else{
								prevExists=false;
							}
						}
					}
					if (testNum>=A && testNum<=B && testNum>j && prevExists==false){
						numRecycledPairs++;
						prevMatches.add(new Integer(testNum));
					}
				}
			}
			System.out.printf("Case #%d: %d", i+1,numRecycledPairs);
			if (i!=numCases-1){
				System.out.println();
			}
		}
	}


	//credit to Marian	
	//http://stackoverflow.com/questions/1306727/way-to-get-number-of-digits-in-an-int
	int numDigits(int n){
		if (n < 100000)
		{
			// 5 or less
			if (n < 100)
			{
				// 1 or 2
				if (n < 10)
					return 1;
				else
					return 2;
			}
			else
			{
				// 3 or 4 or 5
				if (n < 1000)
					return 3;
				else
				{
					// 4 or 5
					if (n < 10000)
						return 4;
					else
						return 5;
				}
			}
		}
		else
		{
			// 6 or more
			if (n < 10000000)
			{
				// 6 or 7
				if (n < 1000000)
					return 6;
				else
					return 7;
			}
			else
			{
				// 8 to 10
				if (n < 100000000)
					return 8;
				else
				{
					// 9 or 10
					if (n < 1000000000)
						return 9;
					else
						return 10;
				}
			}
		}
	}
}

