/*
 * 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.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

public class RecycledNumber {

	public static void main(String[] args) {

		try {

			FileInputStream fstream = new FileInputStream(args[0]);

			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;

			int count = 0;
			int noCases = 0;
			while ((strLine = br.readLine()) != null) {
				if (count > noCases)
					break;

				if (count == 0) {
					noCases = Integer.valueOf(strLine);
				} else {
					compute(count, strLine);
				}

				count++;
			}

			in.close();
		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}
	}

	private static void compute(int caseNo, String str) {
		String[] AB = str.split(" ");
		int A = Integer.valueOf(AB[0]);
		int B = Integer.valueOf(AB[1]);

		int meetsCriteria = 0;

		HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
		while (A < B) {
			hm = new HashMap<Integer, Integer>();
			int n = A;

			String nStr = String.valueOf(n);
			int nLen = nStr.length();

			for (int j = 0; j < nLen - 1; j++) {

				nStr = nStr.charAt(nLen - 1) + nStr.substring(0, nLen - 1);
				int m = Integer.valueOf(nStr);

				if (Integer.valueOf(AB[0]) <= n && n < m && m <= B) {

					if (!hm.containsKey(m)) {
						hm.put(m, m);
						meetsCriteria++;
					}

				}
			}

			A++;
		}

		System.out
				.println(String.format("Case #%s: %s", caseNo, meetsCriteria));

	}
}
