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

}

package hk.polyu.cslhu.codejam.thread;

import hk.polyu.cslhu.codejam.lib.ResultCollector;
import hk.polyu.cslhu.codejam.solution.Solution;

import java.util.List;

import org.apache.log4j.Logger;

public abstract class JamThread implements Runnable {
	public static Logger logger = Logger.getLogger(JamThread.class);

	private ResultCollector resultCollector;
	private int indexOfList;
	private List<List<String>> testCaseList;
	private Solution solution;
	
	/**
	 * Return the solution
	 * 
	 * @return Solution
	 */
	public Solution getSolution() {
		return solution;
	}

	/**
	 * Set the solution used for test cases
	 * 
	 * @param solution Solution
	 */
	public void setSolution(Solution solution) {
		this.solution = solution;
	}

	/**
	 * Return the result collector
	 * 
	 * @return ResultCollector
	 */
	public ResultCollector getResultCollector() {
		return resultCollector;
	}

	/**
	 * Set the result collector
	 * 
	 * @param resultCollector ResultCollector
	 */
	public void setResultCollector(ResultCollector resultCollector) {
		this.resultCollector = resultCollector;
	}
	
	/**
	 * Return the index of list
	 *  
	 * @return int The index of list
	 */
	public int getIndexOfList() {
		return indexOfList;
	}

	/**
	 * Set the index of list
	 * 
	 * @param indexOfList int The index of list
	 */
	public void setIndexOfList(int indexOfList) {
		this.indexOfList = indexOfList;
	}

	/**
	 * Set the list of test cases
	 * 
	 * @param testCaseList List<LIst<String>>
	 */
	public void setTestCaseList(List<List<String>> testCaseList) {
		this.testCaseList = testCaseList;
		logger.debug("Total " + testCaseList.size() + " test cases have been added");
	}
	
	/**
	 * Get the list of test cases
	 * 
	 * @return List<List<String>> The list of test cases
	 */
	public List<List<String>> getTestCaseList() {
		return this.testCaseList;
	}
	
	public abstract void run();
}
