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

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class RecycledNumbers {
	BufferedReader reader;
	char [] charInput;
	String [] inputArray;
	int index;
	RecycledNumbers()throws Exception{
		String input=null;
		reader = new BufferedReader(new InputStreamReader(System.in));
		while(reader.ready()){
			if (input==null){
				input =reader.readLine();
			}
			else{
				input =input+ " " + reader.readLine();
			}
		}
		inputArray=input.trim().split(" ");
		index=0;
	}
	String nextInput()throws Exception{
		if(index<inputArray.length){
			int temp=index;
			index++;
			return inputArray[temp];
		}
		else{
			return "\0";
		}
	}
	
	
	void start()throws Exception{
		LinkedList<Integer> prevMatches=new LinkedList<Integer>();
		int numCases=Integer.parseInt(nextInput());
		int A;
		int B;
		int numRecycledPairs=0;
		int [] numArray;
		int digits;
		int testNum=0;
		boolean prevExists=false;
		for(int i=0;i<numCases;i++){
			A=Integer.parseInt(nextInput());
			B=Integer.parseInt(nextInput());
			digits=numDigits(A);
			numRecycledPairs=0;
			for(int j=A;j<=B;j++){
				prevMatches.clear();
				numArray=new int[digits*2-1];
				testNum=j;
				for (int k=numArray.length-1;k>=0;k--){
					testNum=testNum/10;
					if(k==digits-1){
						testNum=j;
					}
					numArray[k]=testNum%10;
				}
				for (int k=1;k<digits;k++){
					testNum=0;
					for (int l=k;l<digits+k;l++){
						testNum=testNum*10;
						testNum=testNum+numArray[l];
					}
					if (prevMatches.size()==0){
						prevExists=false;
					}
					else{
						for (Integer m: prevMatches){
							if (m.intValue()==testNum){
								prevExists=true;
								break;
							}
							else{
								prevExists=false;
							}
						}
					}
					if (testNum>=A && testNum<=B && testNum>j && prevExists==false){
						numRecycledPairs++;
						prevMatches.add(new Integer(testNum));
					}
				}
			}
			System.out.printf("Case #%d: %d", i+1,numRecycledPairs);
			if (i!=numCases-1){
				System.out.println();
			}
		}
	}


	//credit to Marian	
	//http://stackoverflow.com/questions/1306727/way-to-get-number-of-digits-in-an-int
	int numDigits(int n){
		if (n < 100000)
		{
			// 5 or less
			if (n < 100)
			{
				// 1 or 2
				if (n < 10)
					return 1;
				else
					return 2;
			}
			else
			{
				// 3 or 4 or 5
				if (n < 1000)
					return 3;
				else
				{
					// 4 or 5
					if (n < 10000)
						return 4;
					else
						return 5;
				}
			}
		}
		else
		{
			// 6 or more
			if (n < 10000000)
			{
				// 6 or 7
				if (n < 1000000)
					return 6;
				else
					return 7;
			}
			else
			{
				// 8 to 10
				if (n < 100000000)
					return 8;
				else
				{
					// 9 or 10
					if (n < 1000000000)
						return 9;
					else
						return 10;
				}
			}
		}
	}
}

