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

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.util.ArrayList;

public class TestCaseIO {

    public static ArrayList<TestCase> loadFromFile(String infile) {
        //inc checks on #lines, length of splits, etc.
        BufferedReader br = Utils.newBufferedReader(infile);
        ArrayList<TestCase> tcList = new ArrayList<>();

        //first line is N, the number of test cases
        Integer N = Utils.readInteger(br);

        //cycle through N test cases
        for (int x = 0; x < N; x++) {
            TestCase tc = new TestCase();
            tc.setRef(x + 1);

            ///////////////////////////////////////////////////////////
            //load properties - the bit to customise per problem
            ArrayList<Integer> l = Utils.readIntegerList(br, 2);
            tc.setIntegerList("a_b", l);

            ///////////////////////////////////////////////////////////

            tcList.add(tc);
        }

        //confirm end of file
        if (Utils.readLn(br) != null) {
            Utils.die("Unexpected text at end of infile");
        }

        return tcList;
    }

    public static ArrayList<TestCase> mockUp() {

        ArrayList<TestCase> tcList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            TestCase tc = new TestCase();
            tc.setRef(i);
            
            int[] xs = Utils.randomIntegerArray(100, -50, 50, i * 2);
            int[] ys = Utils.randomIntegerArray(100, -50, 50, i * 2 + 1);
            tc.setIntegerList("xs", Utils.arrayToArrayList(xs));
            tc.setIntegerList("ys", Utils.arrayToArrayList(ys));

            tcList.add(tc);
        }
        return tcList;
    }

    public static void writeSolutions(ArrayList<TestCase> tcList, String outfile) {

        BufferedWriter bw = Utils.newBufferedWriter(outfile, false);

        for (int i = 0; i < tcList.size(); i++) {
            TestCase tc = tcList.get(i);
            Object solution = tc.getSolution();
            String line = "Case #" + (i + 1) + ": " + solution.toString();
            Utils.writeLn(bw, line);
        }

        Utils.closeBw(bw);

    }
}
