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

/**
 *
 * @author eblanco
 */
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import javax.swing.JFileChooser;

public class RecycledNumbers {

    public static String DatosArchivo[] = new String[10000];

    public static int[] convertStringArraytoIntArray(String[] sarray) throws Exception {
        if (sarray != null) {
            int intarray[] = new int[sarray.length];
            for (int i = 0; i < sarray.length; i++) {
                intarray[i] = Integer.parseInt(sarray[i]);
            }
            return intarray;
        }
        return null;
    }

    public static boolean CargarArchivo(String arch) throws FileNotFoundException, IOException {
        FileInputStream arch1;
        DataInputStream arch2;
        String linea;
        int i = 0;

        if (arch == null) {
            //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
            int rc = fc.showDialog(null, "Select a File");
            if (rc == JFileChooser.APPROVE_OPTION) {
                arch = fc.getSelectedFile().getAbsolutePath();
            } else {
                System.out.println("No hay nombre de archivo..Yo!");
                return false;
            }
        }
        arch1 = new FileInputStream(arch);
        arch2 = new DataInputStream(arch1);
        do {
            linea = arch2.readLine();
            DatosArchivo[i++] = linea;
            /* if (linea != null) {
            System.out.println(linea);
            }*/
        } while (linea != null);
        arch1.close();
        return true;
    }

    public static boolean GuardaArchivo(String arch, String[] Datos) throws FileNotFoundException, IOException {
        FileOutputStream out1;
        DataOutputStream out2;
        int i;
        out1 = new FileOutputStream(arch);
        out2 = new DataOutputStream(out1);

        for (i = 0; i < Datos.length; i++) {
            if (Datos[i] != null) {
                out2.writeBytes(Datos[i] + "\n");
            }
        }
        out2.close();
        return true;
    }

    public static void echo(Object msg) {
        System.out.println(msg);
    }

    public static void main(String[] args) throws IOException, Exception {
        String[] res = new String[10000], num = new String[2];
        int i, j, k, ilong;
        int ele = 1;
       ArrayList al = new ArrayList();
        String FName = "C-small-attempt0",  istr, irev, linea;
        if (CargarArchivo("C:\\eblanco\\Desa\\CodeJam\\Code Jam\\test\\" + FName + ".in")) {
            for (j = 1; j <= Integer.parseInt(DatosArchivo[0]); j++) {
                linea = DatosArchivo[ele++];
                num = linea.split(" ");
                   al.clear();
                // echo("A: "+num[0]+" B: "+num[1]);
                for (i = Integer.parseInt(num[0]); i <= Integer.parseInt(num[1]); i++) {
                    istr = Integer.toString(i);
                    ilong = istr.length();
                 
                    for (k = 1; k < ilong; k++) {
                        if (ilong > k) {
                            irev = istr.substring(istr.length() - k, istr.length()) + istr.substring(0, istr.length() - k);
                            //echo("caso: " + j + ": isrt: " + istr + " irev: " + irev);
                            if ((Integer.parseInt(irev) > Integer.parseInt(num[0])) && (Integer.parseInt(irev) > Integer.parseInt(istr)) && (Integer.parseInt(irev) <= Integer.parseInt(num[1]))) {
                                al.add("(" + istr + "," + irev + ")");
                            }
                        }
                    }
                }
                HashSet hs = new HashSet();
                hs.addAll(al);
                al.clear();
                al.addAll(hs);
                res[j] = "Case #" + j + ": " + al.size();
                echo(res[j]);
                LeerArchivo.GuardaArchivo("C:\\eblanco\\Desa\\CodeJam\\Code Jam\\test\\" + FName + ".out", res);
            }
        }
    }
}

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

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 *
 * @author Unggul
 */
public class Recycle {

    int count;
    ArrayList<String> inputList1 = new ArrayList<String>();
    ArrayList<String> inputList2= new ArrayList<String>();
    ArrayList<String> outputList = new ArrayList<String>();
    
    public Recycle(){                
    }
    
    public String cycle(String input){
        char[] temp = new char[input.length()];        
        temp[0] = input.charAt(input.length()-1);
        for(int i=0;i<input.length()-1;i++){
            temp[i+1] = input.charAt(i);
        }        
        return new String(temp);
    }
    
    public boolean isRecycle(String a, String b){
        String temp = b;
        if(a.length()!=b.length())
            return false;        
        for(int i=0;i<a.length();i++){            
            if(temp.equals(a))
                return true;
            temp = cycle(temp);
            if(temp.equals(b))
                return false;
        }            
        return false;
    }        
    
    public int countRecycle(String a, String b){
        int total = 0;
        int count = 0;
        int valA = StringToInt(a);
        int valB = StringToInt(b);
        String tempA = a;
        String tempB = add(tempA);
        
        for(int i=valA;i<valB;i++){
            for(int j=i+1;j<=valB;j++){
                if(isRecycle(tempA, tempB))
                    total++;
                    
                tempB = add(tempB);
            }
            tempA = add(tempA);            
            tempB = add(tempA);
        }
//        System.out.println(total);
        return total;
    }
    
    public ArrayList<String> countRecycle(ArrayList<String> a, ArrayList<String> b){
        ArrayList<String> tempList = new ArrayList<String>();
        String tempA;
        String tempB;
        int temp;
        for(int i=0;i<count;i++){
            tempA = a.get(i);
            tempB = b.get(i);
            
            temp = countRecycle(tempA, tempB);
            tempList.add(""+temp);            
        }        
        return tempList;
    }
    
    
    
    public String add(String a){
        int i = StringToInt(a);
        i++;
        return new String("" + i);
    }
    
     public int StringToInt(String s){        
        int temp = 0;
        for(int i=0;i<s.length();i++){            
            temp += CharToInt(s.charAt(i));
            temp = temp * 10;
        }
        temp = temp/10;
        return temp;        
    }
     
    public int CharToInt(char c){        
        int temp = 0;
        switch (c) {
            case '0' : temp = 0; break;                
            case '1' : temp = 1; break;                
            case '2' : temp = 2; break;                
            case '3' : temp = 3; break;                
            case '4' : temp = 4; break;                
            case '5' : temp = 5; break;                
            case '6' : temp = 6; break;                
            case '7' : temp = 7; break;                
            case '8' : temp = 8; break;                
            case '9' : temp = 9; break;                                        
        }
        return temp;
    }     
    
    public void getInput(File file) throws FileNotFoundException, IOException{
        // Open the file that is the first
        // command line parameter
        FileInputStream fstream = new FileInputStream(file);
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));        
        String strLine;
        
        strLine = br.readLine();
        count = StringToInt(strLine);//get count
        
        for (int i = 0; i < count; i++) {//get list of string
            strLine = br.readLine();
            String[] temp = strLine.split(" ");
            inputList1.add(temp[0]);
            inputList2.add(temp[1]);
        }
        
        in.close();        
    } 
    
    public void writeOutput() throws IOException{        
        FileWriter fstream = new FileWriter("output2.txt");        
        BufferedWriter out = new BufferedWriter(fstream);                    
        for(int i=0;i<count;i++){
            int x = i+1;
            //System.out.println(x);
            out.write("Case #" + x + ": " + outputList.get(i) + "\n");
        }
        
        out.close();
    }
    
    public void Solve(File file) throws FileNotFoundException, IOException{
        this.getInput(file);//get input
        outputList = this.countRecycle(inputList1, inputList2);
        this.writeOutput();
    }
    
    public static void main(String args[]) throws FileNotFoundException, IOException {
        Recycle r = new Recycle();
        String path = "input.txt";//change this!!
        File f = new File(path);
        r.Solve(f);
    }
    
}
