/*
 * 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 codejam.morl99.c;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class ProblemC {
    public void solveSet(PrintWriter pw, BufferedReader reader) throws IOException {

        String firstLine = reader.readLine();
        Scanner scanner = new Scanner(firstLine);
        scanner.useDelimiter(" ");
        int T = scanner.nextInt();

        for (int i = 1; i <= T; i++) {
            scanner = new Scanner(reader.readLine());
            scanner.useDelimiter(" ");
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            System.out.println("-- n: " + n + " m: " + m);
            int solution = solveLine(n, m);
            pw.printf("Case #%d: %d", i, solution);
            System.out.println("Solved " + i);
            pw.println();
        }

    }

    public int solveLine(int A, int B) {
        int oldSize = 0;
        Set<Pair> pairs = new HashSet<Pair>();
        for (int i = A; i <= B; i++) {
            Set<Pair> newPairs = getPermutations(i);
            for (Pair p: newPairs) {
                if (p.isInBoundaries(A, B)) {
                    pairs.add(p);
                }
                
            }
            if (pairs.size() > oldSize) {
                oldSize = pairs.size();
//                System.out.println("New Pairs for "+ i + ": " + setToString(pairs) ) ;
            }
        }
        
        return pairs.size();
    }
    
    public Set<Pair> getPermutations(int i){
        Set<Pair> pairs = new HashSet<>();
        String number = "" + i;
        
        //we will now create the pairs by cutting the number in half and moving the second half in front of the first
        int position = number.length()-1;
        final int endIndex = number.length();
        while (position > 0) {
            String recycled = number.substring(position, endIndex) + number.substring(0, position);
            int r= Integer.parseInt(recycled);
            if (i != r) {
                pairs.add(new Pair(i,r));
//                System.out.println(number + " <-> " + recycled);
            }
            position--;
        }
        return pairs;
    }

    public static String setToString(Set<Pair> set) {
        StringBuilder s = new StringBuilder();
        for (Pair p:set) {
            s.append(p);
        }
        return s.toString();
    }
    
    public static void main(String[] args) throws IOException {
        ProblemC p = new ProblemC();

        OutputStream output = new FileOutputStream("res/C.out", false);
        PrintWriter pw = new PrintWriter(output);
        BufferedReader reader = new BufferedReader(new FileReader("res/C-small-attempt0.in"));
        p.solveSet(pw, reader);
        pw.close();
        reader.close();

    }
}

class Pair {
    int n, m;

    public Pair(int x, int y) {
        if (x < y) {
            n = x;
            m = y;
        } else {
            m = x;
            n = y;
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Pair) {
            Pair other = (Pair) obj;
            if (n == other.n && m == other.m)
                return true;
            if (n == other.m && m == other.n)
                return true;
            return false;
        }
        return super.equals(obj);
    }

    public boolean isInBoundaries(int A, int B) {
        return n >= A && m <= B;
    }

    @Override
    public int hashCode() {
        return n+m;
    }
    
    @Override
    public String toString() {
        return "("+n+","+m+")";
    }
}