import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

public class ReadFile {
	public static void main(String[] args) throws Exception {
		String srcFile = "C:" + File.separator + "Documents and Settings"
				+ File.separator + "rohit" + File.separator + "My Documents"
				+ File.separator + "Downloads" + File.separator
				+ "C-small-attempt1.in.txt";
		String destFile = "C:" + File.separator + "Documents and Settings"
				+ File.separator + "rohit" + File.separator + "My Documents"
				+ File.separator + "Downloads" + File.separator
				+ "C-small-attempt0.out";
		File file = new File(srcFile);
		List<String> readData = new ArrayList<String>();
		FileReader fileInputStream = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(fileInputStream);
		StringBuilder stringBuilder = new StringBuilder();
		String line = null;
		while ((line = bufferedReader.readLine()) != null) {
			readData.add(line);
		}
		stringBuilder.append(generateOutput(readData));
		File writeFile = new File(destFile);
		if (!writeFile.exists()) {
			writeFile.createNewFile();
		}
		FileWriter fileWriter = new FileWriter(writeFile);
		BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
		bufferedWriter.write(stringBuilder.toString());
		bufferedWriter.close();
		System.out.println("output" + stringBuilder);
	}

	/**
	 * @param number
	 * @return
	 */
	private static String generateOutput(List<String> number) {
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < number.size(); i++) {
			if (i == 0) {
				continue;
			} else {
				String tempString = number.get(i);
				String[] temArr = tempString.split(" ");
				stringBuilder.append("Case #" + i + ": ");
				stringBuilder.append(getRecNumbers(temArr[0], temArr[1]));
				stringBuilder.append("\n");
			}
		}
		if (stringBuilder.length() > 0) {
			stringBuilder.deleteCharAt(stringBuilder.length() - 1);
		}
		return stringBuilder.toString();
	}

	// /* This method accepts method for google code jam */
	// private static String method(String value) {
	// int intValue = Integer.valueOf(value);
	// double givenExpr = 3 + Math.sqrt(5);
	// double num = Math.pow(givenExpr, intValue);
	// String valArr[] = (num + "").split("\\.");
	// int finalVal = Integer.valueOf(valArr[0]) % 1000;
	//
	// return String.format("%1$03d", finalVal);
	//
	// }
	//
	// /* This method accepts method for google code jam */
	// private static String method2(String value) {
	// StringTokenizer st = new StringTokenizer(value, " ");
	// String test[] = value.split(" ");
	// StringBuffer str = new StringBuffer();
	//
	// for (int i = 0; i < test.length; i++) {
	// // test[i]=test[test.length-i-1];
	// str.append(test[test.length - i - 1]);
	// str.append(" ");
	// }
	// str.deleteCharAt(str.length() - 1);
	// String strReversedLine = "";
	//
	// while (st.hasMoreTokens()) {
	// strReversedLine = st.nextToken() + " " + strReversedLine;
	// }
	//
	// return str.toString();
	// }

	private static int getRecNumbers(String no1, String no2) {

		int a = Integer.valueOf(no1);
		int b = Integer.valueOf(no2);
		Set<String> dupC = new HashSet<String>();
		for (int i = a; i <= b; i++) {
			int n = i;
			List<String> value = findPerm(n);
			for (String val : value) {
				if (Integer.valueOf(val) <= b && Integer.valueOf(val) >= a
						&& n < Integer.valueOf(val)) {
					dupC.add(n + "-" + val);
				}
			}
		}
		return dupC.size();
	}

	private static List<String> findPerm(int num) {
		String numString = String.valueOf(num);
		List<String> value = new ArrayList<String>();
		for (int i = 0; i < numString.length(); i++) {
			String temp = charIns(numString, i);
			if (!temp.equalsIgnoreCase(numString)) {
				value.add(charIns(numString, i));
			}
		}
		return value;

	}

	public static String charIns(String str, int j) {
		String begin = str.substring(0, j);
		String end = str.substring(j);
		return end + begin;
	}
}
/*
 * @(#)CJ2012C.java
 * por Jorge LÃ³pez Palacios
 *
 * MÃ¡s informaciÃ³n y descripciÃ³n del CopyrightÂ©.
 *
 */

/**
 * Clase CJ2012C
 * @author Jorge LÃ³pez Palacios
 * @version 1.0
 */

//***************** PACKAGES ****************************//
package codejam;
//***************** IMPORTS *****************************//
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;


public class CJ2012C {
    //***************** CONSTANTES ****************************//
    private static final String SPATHFILEINPUT = "C:\\C-small-attempt0.in";
    private static final String SPATHFILEOUTPUT = "C:\\CJ2012C.txt";
    //***************** ATRIBUTOS *****************************//
    private static BufferedReader brBuffer;
    private static String sFileLine = "";
    private static PrintWriter pwFileOutput;
    //***************** METODOS SELECTORES ********************//

    //***************** METODOS MODIFICADORES *****************//

    //***************** CONSTRUCTORES *************************//

    //***************** MAIN **********************************//
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int iNumberCases = 0;
        int iCurrentLine = 0;

        int aiNumbers[] = new int[2];
        String asNumbers[] = new String[2];
        String sDummy = "";
        String sNNormal = "";
        //String sNBackward = "";
        int iCountRecycled = 0;
        //String sBackwardDummy = "";

        try{
            // Recogemos los datos del fichero
            brBuffer = getFileToString(SPATHFILEINPUT);

            // Recogemos el fichero de salida
            pwFileOutput = getFile();
            
            // Leemos cada lÃ­nea del fichero
            while((sFileLine = brBuffer.readLine()) != null){
                // Inicializo el contador de nÃºmeros reciclados
                iCountRecycled = 0;

                // Recojo el nÃºmero de casos
                if(iCurrentLine == 0){
                    iNumberCases = Integer.parseInt(sFileLine);
                }else{
                    // Recojo los nÃºmeros
                    asNumbers = sFileLine.split(" ");
                    aiNumbers = new int[asNumbers.length];

                    for(int i = 0; i <= asNumbers.length - 1; i ++){
                        aiNumbers[i] = Integer.parseInt(asNumbers[i]);
                    }

                    // Recojo todos los nÃºmeros de A hasta B
                    for(int n = aiNumbers[0]; n <= aiNumbers[1]; n ++){
                        // Recojo todos los nÃºmeros de B hasta A
                        for(int m = aiNumbers[1]; (m > n); m --){
                            // Compruebo si la combinacion (n, m) es de nÃºmeros reciclados
                            sNNormal = n + "";

                            for(int i = 0; (i <= sNNormal.length() - 1); i ++){
                                sDummy = sNNormal.substring((sNNormal.length() - i), sNNormal.length()) + sNNormal.substring(0, (sNNormal.length() - i));

                                if((Integer.parseInt(sDummy) == m) && ((Integer.parseInt(sDummy) + "").length() == (m + "").length())){
                                    iCountRecycled ++;
                                }
                            }
                        }
                    }
                }

                if(iCurrentLine == 0){
                    System.out.println("Numero de casos: " + iNumberCases);
                }else{
                    printToFile("Case #" + iCurrentLine + ": " + iCountRecycled);
                    //System.out.println("Case #" + iCurrentLine + ": " + iCountRecycled);
                }

                // Incrementamos el nÃºmero de lÃ­nea
                iCurrentLine ++;
            }
            //Cerramos el buffer del fichero
            closeInputStream(brBuffer);
        }catch (Throwable th){
            System.err.println("Error: " + th.toString());
        }
    }
    //***************** METODOS PÃBLICOS **********************//

    //***************** METODOS PRIVADOS **********************//
    /**
     * Devuelve los datos de un fichero indicado por parametro
     *
     * @param ai_sPathFile: Ruta del fichero
     * @throws IOException
     */
    private static BufferedReader getFileToString(String ai_sPathFile) throws IOException{
        // Abrimos el fichero
        FileInputStream fisFile = new FileInputStream(ai_sPathFile);

        // Recojo los datos del fichero
        DataInputStream disData = new DataInputStream(fisFile);
        brBuffer = new BufferedReader(new InputStreamReader(disData));

        return brBuffer;
    }

    /**
     * Cierra el buffer del fichero
     *
     * @param ai_bfrBuffer
     * @throws IOException
     */
    private static void closeInputStream(BufferedReader ai_bfrBuffer) throws IOException{
        if(ai_bfrBuffer != null){
            ai_bfrBuffer.close();
        }
    }

    /**
     * Recoge el fichero de salida en memoria
     *
     * @throws FileNotFoundException
     */
    private static PrintWriter getFile() throws FileNotFoundException{
	pwFileOutput = new PrintWriter(SPATHFILEOUTPUT);
	return pwFileOutput;
    }

    /**
     * Escribe en el fichero de salida
     *
     * @param ai_sText
     */
    public static void printToFile(String ai_sText){
        // Escribo en el fichero
        pwFileOutput.append(ai_sText);
        pwFileOutput.println();
        // Cierro el fichero
        pwFileOutput.flush();
    }

}//FIN DE LA CLASE