import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class Recycle {

	private static HashMap<Integer, HashSet<Integer>> map = new HashMap<Integer, HashSet<Integer>>();
	private static HashSet<Integer> toSkip = new HashSet<Integer>();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int T = reader.nextInt();
		for (int tc = 1; tc <= T; tc++) {
			int a = reader.nextInt();
			int b = reader.nextInt();
			long before = System.currentTimeMillis();
			int res = doit(a, b);
			System.out.printf("Case #%d: %d\n", tc, res);
			long after = System.currentTimeMillis();
//			System.out.println("time taken: " + (after - before));
		}
	}

	private static int doit(int a, int b) {
		int count = 0;
		HashSet<Integer> skip = new HashSet<Integer>(toSkip);
		for (int i = a; i <= b; i++) {
			if (skip.contains(i))
				continue;
			HashSet<Integer> arr = generate(i);
//			if(arr.size() > 1) {
//				map.put(i, arr);
//			}
			// System.out.println(i + " --> " +
			// Arrays.deepToString(arr.toArray()));
			int temp = 0;
			if (arr.size() > 1) {
				for (int x : arr) {
					if (x >= a && x <= b) {
						temp++;
						skip.add(x);
					}
				}
				count += temp * (temp - 1) / 2;
			}
//			System.out.println("not skipping " + i + " gen: " + arr.size()+ " " + (temp * (temp - 1) / 2));
		}
		return count;
	}

	private static HashSet<Integer> generate(int num) {
		if(map.containsKey(num)) {
			HashSet<Integer> list = map.get(num);
			return list;
		}
		HashSet<Integer> list = new HashSet<Integer>();
		String s = "" + num;
		list.add(num);
		int len = s.length();
		for (int i = 1; i < len; i++) {
			String temp = s.substring(i) + s.substring(0, i);
			// System.out.println("temp: " + temp);
			if (temp.charAt(0) == '0')
				continue;
			int x = Integer.parseInt(temp);
			if( x <= 2000000) {
				list.add(x);
			}
		}
		if(list.size() > 1) {
			map.put(num, list);
		} else {
			toSkip.add(num);
		}
		return list;
	}

}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingQueue;


public class RecyclePair {
	
	public static void main(String[] args) {
		runTestCase();
	}
	public static void runTestCase() {
		String fileName = "C-small-attempt0.in";// input file
		String outFileName = "outputFile.txt";// output file

		try {
			FileInputStream fStream = new FileInputStream(fileName);
			FileOutputStream fOutStream = new FileOutputStream(outFileName);

			BufferedReader bf = new BufferedReader(new InputStreamReader(
					fStream));
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
					fOutStream));
			int i = 0;
			int testSize = Integer.parseInt(bf.readLine());
			while (i < testSize) {
				String in = bf.readLine();
				bw.write(checkForMatchingPairs(in, ++i));
				bw.newLine();
			}
			// close the two files.
			bf.close();
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static String checkForMatchingPairs (String inputData , int testCase)
	{
		StringBuffer temp = new StringBuffer();
		temp.append("Case #" + testCase + ": ");
		String[] split = inputData.split(" ");
		int lowerLimit = Integer.parseInt(split[0]);
		int upperLimit = Integer.parseInt(split[1]);
		int noOfMatches =0;
		for(int count=lowerLimit;count<upperLimit;count++)
		{
			int numberPairs = findPairs(count, upperLimit, lowerLimit);
			noOfMatches+=numberPairs;
		}
		temp.append(noOfMatches);
		return temp.toString();
	}
	
	public static int findPairs(int number,int upperLimit , int lowerLimit)
	{
		Queue<Integer> queue = new LinkedBlockingQueue<Integer>();
		Stack<Integer> stack = new Stack<Integer>();
		int divisor = number;
		int noOfMatches = 0;
		while(divisor>0)
		{		
			int remainder = divisor%10;
			divisor = divisor/10;
			stack.push(remainder);
		}
		
		while(!stack.isEmpty())
		{
			queue.add(stack.pop());
		}
		
		Integer count = 0;
		while(count<queue.size())
		{
			StringBuffer newNumber = new StringBuffer();
			for(Integer integer :queue)
			{
				newNumber.append(integer);
			}
			
			int matchingPair = Integer.parseInt(newNumber.toString());
			if(matchingPair > number && matchingPair >= lowerLimit &&  matchingPair <=upperLimit)
			{
				noOfMatches++;
				System.out.println(number +":"+ matchingPair  );
			}
			count++;
			Integer head = queue.poll();
			queue.add(head);
		}
		return noOfMatches;
	}
}
