/*
 * 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.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;


public class G2 
{
	 private Scanner reader;
	 private FileWriter fstream;
	 private BufferedWriter out;
	 String nextline;
	 int noLines;
	 int intA;
	 int intB;
	 int lineIndex=1;
	 int value=0;
	 
	 
	 public boolean isValid(int n,int m)
	 {
		 return (!(n==m)&&(intA<=n)&&(n<m)&&(m<=intB));
		 	
	 }
	 public void initiateFiles()
	{
			try 
			{
				
				reader = new Scanner(new File("src/resources/C-small-attempt0.in"));
				String nextline = reader.nextLine();
				noLines = new Integer(nextline);
				System.out.println(noLines);
				fstream = new FileWriter("out.txt");
				out = new BufferedWriter(fstream);
				
				process();
				out.close();
				
			}
			catch (Exception e) {
				// TODO: handle exception
			}
	}
	 public void process() throws IOException
	 {
		 while (reader.hasNext()) 
		{
			 	value=0;
			 	nextline = reader.nextLine();
			 	System.out.println("Case #"+(lineIndex)+": ");
	            out.write("Case #"+(lineIndex++)+": ");
	            StringTokenizer allwords = new StringTokenizer(nextline);
	            intA= new Integer(allwords.nextToken());
	            intB= new Integer(allwords.nextToken());
	            System.out.println(intA+"    "+intB);
	            
	            int n,m=0;
	            for(n=intA;n<=intB;n++)
	            {
	            	System.out.println(n+"    "+m);
	            	String ns=""+n;
	            	
	            	for(int i=1;i<=ns.length();i++)
	            	{
	            		//System.out.println(i);
	            		String S2=ns.substring(ns.length()-i);
	            		
	            		if(S2.charAt(0)=='0')
	            			continue;
	            		String S1=ns.substring(0,ns.length()-i);
	            		
	            		String ms=S2+S1;
	            		m=new Integer(ms);
	            		//System.out.println(isValid(n, m));
	            		if(isValid(n, m))
	            		{
	            			value++;
	            		}
	            		
	            	}
	            }
	            out.write(value+"\n");
	            

	       }
	 }
	 
	public static void main(String []args)
	{
		G2 obj=new G2();
		obj.initiateFiles();
		
		
		
	}

}
