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

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codejam;
import java.io.*;
/**
 *
 * @author Nah-Abiah
 */
public class recycledNumbers {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try{
            FileInputStream fstream = new FileInputStream("C-small-attempt1.in");
            
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            
            int testCount = 0;
            int caseCount = 1;
            String writeFile = "";
            while ((strLine = br.readLine()) != null)   {
                if(testCount == 0){
                    testCount = Integer.parseInt(strLine);
                } 
                else{                    
                    String[] ints = strLine.split(" ");
                    int n = Integer.parseInt(ints[0]);
                    int limit = Integer.parseInt(ints[1]);                    
                    int num = 0;
                    if(ints[0].length() != 1){
                        while (n < limit){
                            int count = 1;
                            
                            while(count < ints[0].length()){
                                String m = String.valueOf(n).substring(count)+String.valueOf(n).substring(0, count);
                                if((n<Integer.parseInt(m)) && (Integer.parseInt(m)<= limit)){
                                    num++;
                                }
                                count++;
                            }
                            n++;
                        }
                    }
                    writeFile += "Case #"+caseCount+": "+num+"\r\n";
                    caseCount++;
                }
            }
            //Close the input stream
            in.close();
            
            try{
                // Create file 
                FileWriter filestream = new FileWriter("output.txt");
                BufferedWriter out = new BufferedWriter(filestream);
                out.write(writeFile);
                //Close the output stream
                out.close();
            }
            catch (Exception e){//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }
        }
        catch (IOException | NumberFormatException e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
}
