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

package year2012.qualification.c;

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class RecycledNumbers {
	public static void main(String[] args) throws Exception{
		//GCJå±é
		String filename = "src/year2012/qualification/c/C-small-attempt0";
		PrintWriter out = new PrintWriter(new File(filename + ".out"));
		Scanner scan = new Scanner(new File(filename + ".in"));
		
		final int T = scan.nextInt();
		for(int i=0;i<T;i++){
			int ans=0;
			int A = scan.nextInt();
			int B = scan.nextInt();
			
			for(int n=A;n<B;n++){
				int m_memo = -1;
				
				char[] na = String.valueOf(n).toCharArray();
				//ä¾ãã°nã123ã®ã¨ãã231,312ã[A,B)ã®ä¸­ã«ãããèª¿ã¹ã
				//ãã®ä¾ã ã¨k=2åã«ã¼ã
				for(int k=1;k<na.length;k++){
					char[] ma = rotate(na, k);
					int m = Integer.parseInt(String.valueOf(ma));
					if(A <=n
						&& n < m
						&& m <= B
						&& m != m_memo){
						
						m_memo = m;
						ans++;
					}
				}
				
			}
			//åºå
			out.printf("Case #%d: %s\n", i+1, ans);
		}
		out.flush();
	}
	
	
	/**
	 * éåãnåå·¦ã«ããããæ°ããéåãè¿ãã¾ãã
	 * @param in
	 * @param n
	 * @return
	 */
	private static char[] rotate(final char[] in, int n){
		if(n>=in.length){
			throw new IllegalArgumentException("n= " + n + ", in.length = "
					+ in.length + "ã¨ãªã£ã¦ãã¾ããn<in.lengthã«ãã¦ãã ããã");
		}
		char[] out = new char[in.length];
		
		//1,2,3,4ã2åå·¦ã«ãããå ´åãã¾ãå·¦å´ã®2åãéé¿ãã
		char[] tmp = new char[n];
		for(int i=0;i<n;i++){
			tmp[i] = in[i];
		}
		//æ¬¡ã«in[n]ããin[in.length]ã¾ã§ãout[0]ããout[in.length - 2]ã¾ã§ã«ã³ãã¼
		for(int i=0;i<in.length-n;i++){
			out[i] = in[i+n];
		}
		//æå¾ã«out[length-n,length]ã«tmp[0,n]ããå¤ããã©ã
		for(int i=in.length-n;i<in.length;i++){
			out[i] = tmp[i-in.length+n];
		}
		return out;
	}
}
