/*
 * 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 mgg.problems;

import java.util.ArrayList;
import java.util.List;

import mgg.utils.CombinatoryUtils;
import mgg.utils.FileUtil;
import mgg.utils.StringUtils;
import mgg.utils.Triplet;

/**
 * @author manolo
 * @date 14/04/12
 */
public class ProblemB {

	public final static String EXAMPLE_IN = "B-example.in";
	public final static String EXAMPLE_OUT = "B-example.out";

	public final static String B_SMALL_IN = "B-small-practice.in";
	public final static String B_SMALL_OUT = "B-small-practice.out";

	public final static String B_LARGE_IN = "B-large-practice.in";
	public final static String B_LARGE_OUT = "B-large-practice.out";

	public final static String delim = " ";

	public static String solve(FileUtil util) {
		
		String line = util.getLine();
		List<Integer> listOfArgs = StringUtils.stringWithIntegersToList(line, delim);
		//System.out.println("\tArgs = " + listOfArgs);
		
		int googlers = listOfArgs.get(0);
		System.out.println("\tGooglers = " + googlers);
		
		int surprisingScores = listOfArgs.get(1);
		System.out.println("\tSurprising scores = " + surprisingScores);
		
		int leastScore = listOfArgs.get(2);
		System.out.println("\tMinimum = " + leastScore);
		
		List<Integer> listOfScores = listOfArgs.subList(3, listOfArgs.size());
		System.out.println("\tScores = " + listOfScores);
		
		List<Triplet> listOfTriplets;
		List<List<Triplet>> listOfListOfTriplets = new ArrayList<List<Triplet>>();
		for(int score : listOfScores){
			
			listOfListOfTriplets.add(CombinatoryUtils.possibleTriplets(score));
		}
		
		System.out.println("\tPossible triplets: " + listOfListOfTriplets);
		
		List<List<Triplet>> scenarios = calculateScenarios(listOfListOfTriplets, surprisingScores);
		
		System.out.println("\tPossible scenarios: " + scenarios);

		return null;
	}

	private static List<List<Triplet>> calculateScenarios(
			List<List<Triplet>> listOfListOfTriplets, int surprisingScores) {
		
		return null;
	}

}
