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

import java.util.List;
import java.util.Set;

import util.ListFactory;
import util.SetFactory;
import file.Input;
import file.Output;

public class Recycled {

	public void start() {
		List<String> inputLines = Input.getLines();
		if (inputLines.size() > 0) {
			List<String> outputLines = ListFactory.newArrayListInstance();
			int t = Integer.parseInt(inputLines.get(0));
			for (int i = 1; i <= t; i++) {
				String[] parts = inputLines.get(i).split(" ");
				int a = Integer.parseInt(parts[0]);
				int b = Integer.parseInt(parts[1]);
				outputLines.add("Case #" + i + ": " + getNumber(a, b));
			}
			Output.writeLines(outputLines);
		}
	}

	private int getNumber(int a, int b) {
		Set<Pair> set = SetFactory.newHashSetInstance();
		if (a <= b) {
			for (int n = a; n <= b; n++) {
				int length = getLength(n);
				for (int i = 0; i < length - 1; i++) {
					int m = moveRight(n, i + 1);
					if (n < m && m <= b) {
						set.add(new Pair(n, m));
					}
				}
			}
		}
		return set.size();
	}

	private int getLength(int number) {
		int length = 0;
		if (number >= 0 && number <= 9) {
			length = 1;
		} else if (number >= 10 && number <= 99) {
			length = 2;
		} else if (number >= 100 && number <= 999) {
			length = 3;
		} else if (number >= 1000 && number <= 9999) {
			length = 4;
		} else if (number >= 10000 && number <= 99999) {
			length = 5;
		}
		return length;
	}

	private int moveRight(int n, int times) {
		String number = String.valueOf(n);
		for (int i = 0; i < times; i++) {
			int length = number.length();
			number = number.substring(length - 1, length)
					+ number.substring(0, length - 1);
		}
		return Integer.parseInt(number);
	}

	public static void main(String[] args) {
		new Recycled().start();
	}

}
