/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package recycled;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.TreeSet;

/**
 *
 * @author Alfred
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    try {
            FileInputStream fstream = new FileInputStream("C-small-attempt0.in");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            FileWriter outFile = new FileWriter("out.txt");
            PrintWriter out = new PrintWriter(outFile);
            String line;
            line = br.readLine();
            int cases = Integer.parseInt(line);
            for (int i=1; i<=cases;i++){
                line = br.readLine();
                String[] values = line.split(" ");
                int A = Integer.parseInt(values[0]);
                int B = Integer.parseInt(values[1]);
                int total=0;
                for (int n=A; n<=B;n++){
                    TreeSet<String> solutions = new TreeSet<String>();
                    for (int k=1;k<values[1].length();k++){
                        String m = circshift(n,k);
                        int mVal = Integer.parseInt(m);
                        if(mVal<=B && mVal!=n  && mVal> n && !m.startsWith("0") && m.length()==Integer.toString(n).length() )
                        {
                            solutions.add(m);
                        }                   
                    }
                    total+=solutions.size();
                }
                out.println("Case #"+i+": "+total);
            }
            in.close();
            out.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

}

    private static String circshift(int n, int k) {
        String nString=Integer.toString(n);
        int len=nString.length();
        String m =  nString.substring(len-k)+nString.subSequence(0, len-k);
        //System.out.println(n+" "+m);
        return m;
    }

}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingQueue;


public class RecyclePair {
	
	public static void main(String[] args) {
		runTestCase();
	}
	public static void runTestCase() {
		String fileName = "C-small-attempt0.in";// input file
		String outFileName = "outputFile.txt";// output file

		try {
			FileInputStream fStream = new FileInputStream(fileName);
			FileOutputStream fOutStream = new FileOutputStream(outFileName);

			BufferedReader bf = new BufferedReader(new InputStreamReader(
					fStream));
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
					fOutStream));
			int i = 0;
			int testSize = Integer.parseInt(bf.readLine());
			while (i < testSize) {
				String in = bf.readLine();
				bw.write(checkForMatchingPairs(in, ++i));
				bw.newLine();
			}
			// close the two files.
			bf.close();
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static String checkForMatchingPairs (String inputData , int testCase)
	{
		StringBuffer temp = new StringBuffer();
		temp.append("Case #" + testCase + ": ");
		String[] split = inputData.split(" ");
		int lowerLimit = Integer.parseInt(split[0]);
		int upperLimit = Integer.parseInt(split[1]);
		int noOfMatches =0;
		for(int count=lowerLimit;count<upperLimit;count++)
		{
			int numberPairs = findPairs(count, upperLimit, lowerLimit);
			noOfMatches+=numberPairs;
		}
		temp.append(noOfMatches);
		return temp.toString();
	}
	
	public static int findPairs(int number,int upperLimit , int lowerLimit)
	{
		Queue<Integer> queue = new LinkedBlockingQueue<Integer>();
		Stack<Integer> stack = new Stack<Integer>();
		int divisor = number;
		int noOfMatches = 0;
		while(divisor>0)
		{		
			int remainder = divisor%10;
			divisor = divisor/10;
			stack.push(remainder);
		}
		
		while(!stack.isEmpty())
		{
			queue.add(stack.pop());
		}
		
		Integer count = 0;
		while(count<queue.size())
		{
			StringBuffer newNumber = new StringBuffer();
			for(Integer integer :queue)
			{
				newNumber.append(integer);
			}
			
			int matchingPair = Integer.parseInt(newNumber.toString());
			if(matchingPair > number && matchingPair >= lowerLimit &&  matchingPair <=upperLimit)
			{
				noOfMatches++;
				System.out.println(number +":"+ matchingPair  );
			}
			count++;
			Integer head = queue.poll();
			queue.add(head);
		}
		return noOfMatches;
	}
}
