package jp.funnything.competition.util;

import java.math.BigInteger;

/**
 * Utility for BigInteger
 */
public class BI {
    public static BigInteger ZERO = BigInteger.ZERO;
    public static BigInteger ONE = BigInteger.ONE;

    public static BigInteger add( final BigInteger x , final BigInteger y ) {
        return x.add( y );
    }

    public static BigInteger add( final BigInteger x , final long y ) {
        return add( x , v( y ) );
    }

    public static BigInteger add( final long x , final BigInteger y ) {
        return add( v( x ) , y );
    }

    public static int cmp( final BigInteger x , final BigInteger y ) {
        return x.compareTo( y );
    }

    public static int cmp( final BigInteger x , final long y ) {
        return cmp( x , v( y ) );
    }

    public static int cmp( final long x , final BigInteger y ) {
        return cmp( v( x ) , y );
    }

    public static BigInteger div( final BigInteger x , final BigInteger y ) {
        return x.divide( y );
    }

    public static BigInteger div( final BigInteger x , final long y ) {
        return div( x , v( y ) );
    }

    public static BigInteger div( final long x , final BigInteger y ) {
        return div( v( x ) , y );
    }

    public static BigInteger mod( final BigInteger x , final BigInteger y ) {
        return x.mod( y );
    }

    public static BigInteger mod( final BigInteger x , final long y ) {
        return mod( x , v( y ) );
    }

    public static BigInteger mod( final long x , final BigInteger y ) {
        return mod( v( x ) , y );
    }

    public static BigInteger mul( final BigInteger x , final BigInteger y ) {
        return x.multiply( y );
    }

    public static BigInteger mul( final BigInteger x , final long y ) {
        return mul( x , v( y ) );
    }

    public static BigInteger mul( final long x , final BigInteger y ) {
        return mul( v( x ) , y );
    }

    public static BigInteger sub( final BigInteger x , final BigInteger y ) {
        return x.subtract( y );
    }

    public static BigInteger sub( final BigInteger x , final long y ) {
        return sub( x , v( y ) );
    }

    public static BigInteger sub( final long x , final BigInteger y ) {
        return sub( v( x ) , y );
    }

    public static BigInteger v( final long value ) {
        return BigInteger.valueOf( value );
    }
}

/*
 * @(#)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