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

}

package nithin.codejam;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Solution {
	static int valid_pairs[][];
	static int no_valid_pairs=0;
	public static void main(String args[]) throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int t,i;
		t=Integer.parseInt(br.readLine());
		
		for(i=0;i<t;i++)
		{
			st=new StringTokenizer(br.readLine());
			int a,b;
			int result=0;
			a=Integer.parseInt(st.nextToken());
			b=Integer.parseInt(st.nextToken());
			
			no_valid_pairs=0;
			valid_pairs=new int[1000][2]; // Will not work for Large Input
			boolean ex1=true,ex2=true,ex3=true;
			
			if(b<=10)
			{
				if(b!=10)
				{
					ex1=false;
					ex2=false;
					ex3=false;
					result=0;
				}
				else
				{
					if(a==1)
					{
						ex1=false;
						ex2=false;
						ex3=false;
						result=1;
					}
					
				}
			}
			else if(b<=100)
			{
				if(b!=100)
				{
					ex1=true;
					ex2=false;
					ex3=false;
					result=0;
				}
				else
				{
					if(a==1)
					{
						//Extracting 2 digits from 100 will give 1
						ex1=true;
						ex2=false;
						ex3=false;
						result=1;
					}
				}
			}
			else if(b<=1000)
			{
				if(b!=1000)
				{
					ex1=true;
					ex2=true;
					ex3=false;
					result=0;
				}
				else
				{
					if(a==1)
					{
						//Extracting 3 digits from 1000 will give 1
						ex1=true;
						ex2=true;
						ex3=false;
						result=1;
					}
				}
			}
			else
			{
				ex1=true;
				ex2=true;
				ex3=true;
			}
			
			if(ex1==true)
			{
				int j;
				for(j=a;j<=b;j++)
				{
					int last=j%10;
					int rem=j/10;
					int num=-1;
					if(rem!=0)
					{
						if(rem<10)
						{
							num=last*10+rem;
						}
						else if(rem<100)
						{
							num=last*100+rem;
						}
						else if(rem<1000)
						{
							num=last*1000+rem;
						}
						else
						{
							System.out.println("Not a Valid Case for Small Input");
						}
						if(num>=a && num<=b && j!=num)
						{
							if(j<=num)
							{
								if(add(j,num))
									result++;
							}
							else
							{
								if(add(num,j))
									result++;
							}
						}
					}
					

				}
			}
			if(ex2==true)
			{
/*				int j;
				for(j=101;j<=b;j++)
				{
					int last=j%100;
					int rem=j/100;
					int num=-1;
					if(rem!=0)
					{
						if(rem<10) //if rem=10 then it means it was obtained from a number like 1025 by extracting last 2 digits, so you have to multiply last by 100
						{
							num=last*100+rem;
						}
						else if(rem<100)
						{
							num=last*1000+rem;
						}
						else if(rem<1000)
						{
							num=last*10000+rem;
						}
						else
						{
							System.out.println("Not a Valid Case for Small Input");
						}
						if(num>=a && num<=b && j!=num)
						{

							if(add(j,num)) //num is actually m, it is obtained by moving some digits from back of j to the front.
								result++;

						}
					}
					

				}*/
			}
			if(ex3==true)
			{
				//Do Nothing
			}
			System.out.println("Case #"+(i+1)+": "+result);
/*			int j;
			for(j=0;j<no_valid_pairs;j++)
				System.out.println("0th: "+valid_pairs[j][0]+"\t1th: "+valid_pairs[j][1]);*/
		}
	}
	static boolean add(int m,int n)
	{
		//Pass such that m<n
		boolean uniq_pair=true;
		int i;
		
		for(i=0;i<no_valid_pairs;i++)
		{
			int a=valid_pairs[i][0];
			int b=valid_pairs[i][1];
			if((a==m && b==n) || (b==m && a==n))
				return false;
		}
		valid_pairs[no_valid_pairs][0]=m;
		valid_pairs[no_valid_pairs][1]=n;
		no_valid_pairs++;
		return uniq_pair;
	}
}

