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.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class RecycledNumbersSmall {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		int noTestcases;
		
		FileInputStream fis = new FileInputStream("D:/Eclipse Workspaces/ws_hyperbola/Recycled Numbers/src/C-large.in");
		BufferedReader br = new BufferedReader(new InputStreamReader(fis));
		String sLine = br.readLine();
		noTestcases = Integer.parseInt(sLine);
		
		StringBuffer sb = new StringBuffer();
		List<String> outputList = new ArrayList<String>();
		
		for(int i = 1; i <= noTestcases && (sLine = br.readLine()) != null; i++) {
			String[] numLimits = sLine.split(" ");
			int lowerLimit = Integer.parseInt(numLimits[0]);
			int upperLimit = Integer.parseInt(numLimits[1]);
			int noRecycledPairs = getRecycledpairsCount(lowerLimit, upperLimit);
			System.out.println("Case #" + i + ": " + noRecycledPairs);
			outputList.add("Case #" + i + ": " + noRecycledPairs);
		}
		
		
		writeOutput(outputList);
		
		br.close();
		fis.close();
		
	}
	
	private static void writeOutput(List<String> lines) throws Exception {
		FileWriter fw = new FileWriter("D:/Eclipse Workspaces/ws_hyperbola/Recycled Numbers/src/C-large-attempt0.out");
		PrintWriter pw = new PrintWriter(fw);
		for(String line : lines) {
			pw.println(line);
		}
		pw.flush();
		pw.close();
	}

	private static int getRecycledpairsCount(int smallerNumber, int biggerNumber) {
		int result = 0;
		
		if(biggerNumber < 10) return 0;	// 1 digit numbers
		
		
		if(isBetween(smallerNumber, 10, 99)) {	// 2 digit numbers
			Set<RecycledPair> setPair = new HashSet<RecycledPair>();
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 1);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			//System.out.println(setPair.toString());
			result = result + setPair.size();
		}
		
		else if(isBetween(smallerNumber, 100, 999)) {	// 3 digit numbers
			Set<RecycledPair> setPair = new HashSet<RecycledPair>();
			
			/* one digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 1);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			/* two digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 2);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			//System.out.println(setPair.toString());
			result = result + setPair.size();
		}
		
		else if(isBetween(smallerNumber, 1000, 9999)) {	// 4 digit numbers
			Set<RecycledPair> setPair = new HashSet<RecycledPair>();
			
			/* 1 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 1);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			/* 2 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 2);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			
			/* 3 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 3);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			//System.out.println(setPair.toString());
			result = result + setPair.size();
		}
		
		else if(isBetween(smallerNumber, 10000, 99999)) {	// 5 digit numbers
			Set<RecycledPair> setPair = new HashSet<RecycledPair>();
			
			/* 1 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 1);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			/* 2 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 2);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			
			/* 3 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 3);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			/* 4 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 4);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			result = result + setPair.size();
		}
		else if(isBetween(smallerNumber, 100000, 999999)) {	// 6 digit numbers
			Set<RecycledPair> setPair = new HashSet<RecycledPair>();
			
			/* 1 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 1);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			/* 2 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 2);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			
			/* 3 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 3);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			/* 4 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 4);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			/* 5 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 5);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			result = result + setPair.size();
		}
		else if(isBetween(smallerNumber, 1000000, 2000000)) {	// 7 digit numbers
			Set<RecycledPair> setPair = new HashSet<RecycledPair>();
			
			/* 1 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 1);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			/* 2 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 2);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			
			/* 3 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 3);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			/* 4 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 4);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			/* 5 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 5);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			/* 5 digit shift */
			for(int num = smallerNumber; num <= biggerNumber; num++) {
				int recycledNum = shiftDigit(num, 6);
				if(recycledNum != num && isBetween(recycledNum, smallerNumber, biggerNumber)) {
					setPair.add(new RecycledPair(num, recycledNum));
				}
			}
			
			result = result + setPair.size();
		}
		
		return result;
	}
	
	private static boolean isBetween(int number, int lowerLimit, int upperLimit) {
		return number >= lowerLimit && number <= upperLimit;
	}
	
	private static int shiftDigit(int number, int digitsNo) {
		String sNum = String.valueOf(number);
		int index = sNum.length() - digitsNo;
		String sNewNumber = sNum.substring(index) + sNum.substring(0, index);
		return Integer.parseInt(sNewNumber);
	}
	

}
