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

public class Recycled{
public static void main(String args[])
{
try{
File myFile = new File("C-small-attempt0.in");
FileReader fileReader = new FileReader(myFile);

BufferedReader reader = new BufferedReader(fileReader);
FileWriter fileWriter = new FileWriter("C-small-attempt0.out");
BufferedWriter writer = new BufferedWriter(fileWriter);
String line = null;
line = reader.readLine();
int numLines = Integer.parseInt(line);
//writer.write("hello");
for(int i=0;i<numLines;i++)
{
int recyclecount = 0;
line = reader.readLine();
String AB[] = line.split(" ");

int a = Integer.parseInt(AB[0]);
int b = Integer.parseInt(AB[1]);
int length = AB[0].length();

//the initial value of n

for(int n=a;n<b;n++)
{
System.out.println("N: " + n);
//writer.write("N " + n+"\r\n");
//recycle the value by taking the last digit forward and check with the range,and then the last two digits,etc
for(int j=1;j<length;j++){
if(length!=1){
int tail = n%(int)Math.pow(10,j);
int head = n/(int)Math.pow(10,j);
//concatenate the head and tail and form a new number
int newNumber = Integer.parseInt(tail +""+head);

if((newNumber+"").length() == length){
System.out.println(newNumber);
//writer.write("new " + newNumber+"\r\n");
//compare with the range n<m<=B

if(newNumber<=b && newNumber>n)
{
recyclecount++;
System.out.println("RC " + recyclecount);
//writer.write("RC " + recyclecount+"\r\n");

}

}
}
}
}
String out = "Case #" + (i+1) +": "+recyclecount+"\r\n";
writer.write(out);
System.out.println(out);


}
reader.close();
writer.close();

}catch(Exception ex){
ex.printStackTrace();
}
}
}