/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package recyclednumbers;

import java.util.HashSet;

/**
 *
 * @author vandit
 */
public class RecycleNumbers {

    private HashSet<String> numbers = new HashSet<String>();
    private HashSet<String> initialTempNumbers = new HashSet<String>();
    private HashSet<String> finalTempNumbers = new HashSet<String>();

    HashSet<Pair> numberPairs = new HashSet<Pair>();


    private void findRecycledNumbers(int start, int end, int places) {


        for (int i = start; i <= end; i++) {
            String initialNumber = Integer.toString(i);
            //if (!(numbers.contains(initialNumber) || finalTempNumbers.contains(initialNumber))) {

                StringBuffer tempNumber = new StringBuffer(initialNumber);
                int len = tempNumber.length();
                int startIndexToMove = len - places;
                String tempString = tempNumber.substring(startIndexToMove);
                if ((places == 1 && tempString.equals("0")) || tempString.charAt(0) == '0') {
                    continue;
                }
                tempNumber.delete(startIndexToMove, len);
                String finalTempNumber = tempString + tempNumber.toString();

                if (! (finalTempNumber.equals(initialNumber) || Integer.parseInt(finalTempNumber) > end || Integer.parseInt(finalTempNumber) < Integer.parseInt(initialNumber))) {
                    numbers.add(initialNumber);
                    finalTempNumbers.add(finalTempNumber);
                    Pair pair = new Pair(finalTempNumber,initialNumber);
                    numberPairs.add(pair);
                }
            }

        

    }

    public HashSet<Pair> findAllRecycledNumbers(int start, int end) {
        int length = Integer.toString(start).length();
        for (int i = 1; i < length; i++) {
            findRecycledNumbers(start, end, i);
        }
        return numberPairs;
    }
}

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class RecycleSolution {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String inFilename = "C-small-attempt0.in";
		String outFilename = inFilename.substring(0, inFilename.length() - 3)
				+ ".out";
		try {
			Scanner sc = new Scanner(new File(inFilename));
			PrintStream ps = new PrintStream(new FileOutputStream(outFilename));
			int testCases = Integer.parseInt(sc.nextLine());
			for (int i = 1; i <= testCases; i++) {
				// logic
				if (i >= 1) {
					ps.println();
				}
				String data = sc.nextLine();
				String[] parts = data.split(" ");
				int min = Integer.parseInt(parts[0], 10);
				int max = Integer.parseInt(parts[1], 10);
				int pairCount = 0;
				for (int test = min; test < max; test++) {
					List<Integer> orderings = orderingsOf(test);
					for (int ordering : orderings) {
						if (ordering > test && ordering <= max
								&& ordering >= min) {
							if ((ordering + "").length() == (max + "").length())
								pairCount++;
						}
					}
				}
				System.out.println("Case #" + i + ": " + pairCount);
				ps.print("Case #" + i + ": " + pairCount);
			}
			sc.close();
			ps.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	public static List<Integer> orderingsOf(int number) {
		List<Integer> myOrderings = new ArrayList<Integer>();
		String base = "" + number;
		int baselen = base.length();
		for (int i = 0; i < baselen; i++) {
			String thisperm = base.substring(baselen - i);
			if (baselen - i > 0) {
				thisperm += base.substring(0, baselen - i);
			}
			// Strip out 0-starting
			int tplen = ("" + Integer.parseInt(thisperm, 10)).length();
			if (tplen == baselen)
				myOrderings.add(Integer.parseInt(thisperm, 10));
		}
		return myOrderings;
	}

}
