/*
 * 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.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {

		String filename = "recycled-input.in";
		FileReader reader = new FileReader(new File(filename));
		BufferedReader bufferedReader = new BufferedReader(reader);

		File outputFile = new File("recycled-output.in");
		BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));

		int total = Integer.parseInt(bufferedReader.readLine());
		for( int i = 1; i <= total; i++){
			// processa cada linha
			
			StringTokenizer tokenizer = new StringTokenizer(bufferedReader.readLine());
			int a = getNextInteger(tokenizer);
			int b = getNextInteger(tokenizer);

			int recycledPairs = findAllRecycledNumbers(a,b);
			
			String msg = geraOutputMsg(i,recycledPairs);
			System.out.print(msg);
			
			writer.write(msg);
			
		}
		writer.close();

	}

	private static String geraOutputMsg(int i, int recycledPairs) {
		return String.format("Case #%d: %d\n", i, recycledPairs );
	}

	private static Integer getNextInteger(StringTokenizer tokenizer) {
		return Integer.parseInt(tokenizer.nextToken());
	}

	private static Integer findAllRecycledNumbers(Integer initial, Integer limit) {

		int totalRecycledNumbers = 0;
		for (int i = initial; i <= limit; i++){
			int recycledNumbers = findRecycledNumbers(i, initial, limit);
			totalRecycledNumbers += recycledNumbers;
		}
		
		return totalRecycledNumbers;
	}

	private static int findRecycledNumbers(Integer number, Integer initial, Integer limit) {
		
		String initialString = number.toString();

		int recycledNumbers = 0;
		int length = initialString.length();
		
//		List<String> allPermutations = new ArrayList<String>();
		HashSet<String> recycledList = new HashSet<String>();
		for (int i = 0; i < length-1; i++){

			String movedChars = initialString.substring(0, i+1);
			String strPermutationNumber = initialString.replaceFirst(movedChars, "").concat(movedChars);

//			allPermutations.add(strPermutationNumber);
			
			int permutationNumber = Integer.parseInt(strPermutationNumber);
			if (permutationNumber > number && permutationNumber <= limit && !recycledList.contains(strPermutationNumber)){
				recycledList.add(strPermutationNumber);
				recycledNumbers++;
			}

		}
//		if (recycledNumbers > 0){
//			printRecycledNumbers(number, allPermutations, recycledList);
//			printRecycledNumbers(number, recycledList);
//		}
		return recycledNumbers;
	}

//	private static void printRecycledNumbers(Integer number,
//			List<String> recycledList) {
//		System.out.printf("Número (%d): ", number);
//		printList(allPermutations);
//		System.out.print(" -> ");
//		printList(recycledList);
//		System.out.println();
//	}

//	private static void printList(List<String> list) {
//		System.out.printf("[%s",list.get(0));
//		for (int i = 1; i < list.size(); i++ ){
//			String stringNumber = list.get(i);
//			System.out.printf(",%s",stringNumber);
//		}
//		System.out.print("]");
//	}
}
