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 org.ryan.jam;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * user: ryan.moore
 * date: 4/14/12
 */
public class CodeJamUtil {

    public static List<String> parseInputToList(String inputString) {

        String numLines = inputString.substring(0, inputString.indexOf("\n"));

        String remainingString = inputString.substring(inputString.indexOf("\n")+1);

        List<String> linesList = new ArrayList<String>();

        for (int i = 0; i < Integer.valueOf(numLines); i++) {
            if (i < Integer.valueOf(numLines) -1) {
                linesList.add(remainingString.substring(0, remainingString.indexOf("\n") + 1).trim());
                remainingString = remainingString.substring(remainingString.indexOf("\n")+1);
            } else {
                linesList.add(remainingString);
            }
        }

        return linesList;
    }

    public static List<Integer> getDistinctRecycledPairs(List<String> linesList) {

        //each iteration is a testcase
        List<Integer> resultMatchesList = new ArrayList<Integer>();
        for (String line : linesList) {

            Integer numberA = Integer.valueOf(line.substring(0, line.indexOf(" ")));
            Integer numberB = Integer.valueOf(line.substring(line.indexOf(" ") + 1).trim());

            Integer numberMatches = 0;

            for(int i=numberA; i<numberB - 1; i++) {

                for(int j=numberA + 1; j<=numberB; j++) {

                    //ok now we're iterating correctly, lets call a method to get permutations + check matches
                    Integer result = permuteAndCompare(i,j);
                    numberMatches = numberMatches + result;
                }

                numberA++;
            }
            resultMatchesList.add(numberMatches);
        }

        return resultMatchesList;
    }

    public static String readFileAsString(String filePath) throws java.io.IOException {

        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){

            fileData.append(buf, 0, numRead);
        }
        reader.close();
        return fileData.toString();
    }

    private static Integer permuteAndCompare(Integer numberA, Integer numberB) {

        int numberMatches = 0;

        List<Integer> validPermutes = getValidRecycledPermutations(numberB);

        for (Integer current : validPermutes) {
            if(current.equals(numberA)) {
                numberMatches++;

                System.out.println("Found match = "+ numberA + " and " + current);
            }
        }

        return numberMatches;
    }

    private static List<Integer> getValidRecycledPermutations(Integer number) {

        String numberString = Integer.toString(number);

        Integer numDigits = numberString.length();

        List<Integer> resultList= new ArrayList<Integer>();
        for (int i =numDigits-1; i>0; i-- ) {

            char ch4r = numberString.charAt(i);
            if (!(ch4r == '0')) {

                String concatStringResult = numberString.substring(i, numDigits) + numberString.substring(0, i);
                Integer result = Integer.valueOf(concatStringResult.trim());
                resultList.add(result);
            } //else dont do anything

        }

        return resultList;
    }
}
