/*
 * 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.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;


public class RecycledNumbers {
	private static int T;
	private static ArrayList<Integer> lowerLimit, upperLimit;
	private static ArrayList<Integer> numOfPairs;
	
	public static void main(String[] args) {
		readInputFile ();
		
		getNumberOfRecycledPairs ();
		
		writeOutputFile ();
	}
	
	private static void readInputFile () {
		numOfPairs = new ArrayList<Integer> ();
		lowerLimit = new ArrayList<Integer> ();
		upperLimit = new ArrayList<Integer> ();
		String line = "";
		
		try {
			BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (new FileInputStream ("input/C-small-attempt0.in")));
			
			line = bufferedReader.readLine ();
			T = Integer.parseInt (line);
			
			while ((line = bufferedReader.readLine ()) != null) {
				String [] dataSet = line.split (" ");
				
				lowerLimit.add (Integer.parseInt (dataSet [0]));
				upperLimit.add (Integer.parseInt (dataSet [1]));
			}
			
			System.out.println (lowerLimit + "\n" + upperLimit);
			bufferedReader.close ();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static void getNumberOfRecycledPairs () {
		for (int i = 0; i < T; i++) {
			int numOfValidPairs = 0;
			
			for (int j = lowerLimit.get (i); j < upperLimit.get (i); j++) {				
				int numOfDigits = String.valueOf (j).length ();
				//int num = j;
				
				for (int k = 1; k < numOfDigits; k++) {					
					//num = ((num % (int)Math.pow (10, numOfDigits - 1)) * 10 + 
					//		(num / (int)Math.pow (10, numOfDigits - 1)));
					int num = ((j % (int)Math.pow (10, k)) * (int)Math.pow (10, numOfDigits - k)) + (j / (int)Math.pow (10, k));
					
					//if (num / (int)Math.pow (10, numOfDigits - 1) > 0) {
					if (String.valueOf (num).length() == String.valueOf (j).length()) {
						if (j < num && num <= upperLimit.get (i)) {
							numOfValidPairs++;
						}
					}
				}
			}
			numOfPairs.add (numOfValidPairs);
		}
	}
	
	private static void writeOutputFile () {
		try {
			BufferedWriter bufferedWriter = new BufferedWriter (new OutputStreamWriter (new FileOutputStream ("output/output.out")));
			
			for (int i = 0; i < numOfPairs.size (); i++) {
				bufferedWriter.write ("Case #" + (i + 1) + ": " + numOfPairs.get (i) + "\n");
			}
			
			bufferedWriter.close ();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
