import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class CopyOfCopyOfMain {

	static int ndigits(int n) {
		return (int) (Math.log10(n) + 1);
	}

	static int rot(int n, int dig) {
		return (int) ((n % 10) * Math.pow(10, dig - 1)) + (int) (n / 10);
	}

	public static void main(String[] args) throws FileNotFoundException {
		String file = "C-large";
		Scanner in = new Scanner(new File(file + ".in"));
		PrintWriter out = new PrintWriter(new File(file + ".out"));

		int T = in.nextInt();
		for (int t = 1; t <= T; t++) {
			int A = in.nextInt();
			int B = in.nextInt();

			int c = 0;
			int[] list = new int[ndigits(B)];
			numbers: for (int i = A; i <= B; i++) {
				int digs = ndigits(i);
				int rot = i;
				int pairs = 0;

				Arrays.fill(list, -1);
				list[0] = i;

				digits: for (int j = 1; j <= digs + 1; j++) {

					rot = rot(rot, digs);
					if(rot < i)
						continue;

					if (A <= rot && rot <= B) {
						for (int k = 0; k < j; k++) {
							if (list[k] == rot) {
								continue digits;
							}
						}
						pairs++;
						list[j] = rot;
					}
				}

				//c += pairs * (pairs + 1) / 2;
				c += pairs ;
				// c+=digs;
			}
			out.println("Case #" + t + ": " + c);
		}

		in.close();
		out.close();
	}
}

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

}
