package codejam;

import java.util.*;
import java.io.*;

public class RecycledNumbers {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader("in.txt"));
		PrintWriter out = new PrintWriter(new File("out.txt"));
		
		int T = Integer.parseInt(in.readLine());
		for (int t = 1; t <= T; ++t) {
			String[] parts = in.readLine().split("[ ]+");
			int A = Integer.parseInt(parts[0]);
			int B = Integer.parseInt(parts[1]);
			
			int cnt = 0;
			for (int i = A; i < B; ++i) {
				String str = String.valueOf(i);
				int n = str.length();
				String res = "";
				Set<Integer> seen = new HashSet<Integer>();
				for (int j = n - 1; j > 0; --j) {
					res = str.substring(j) + str.substring(0, j);
					int k = Integer.parseInt(res);
					if (k > i && k <= B) {
						//System.out.println("(" + i + ", " + k + ")");
						if (!seen.contains(k)) {
							++cnt;
							seen.add(k);
						}
					}
				}
			}

			out.println("Case #" + t + ": " + cnt);
		}
		
		in.close();
		out.close();
		System.exit(0);
	}
}

package recycling;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
	
	public static int lint(int n){
		if(n == 0)
			return 0;
		return 1 + lint(n / 10);
	}
	
	public static void permut(int[] tab){
		int l = tab.length;
		int temp = tab[l - 1];
		for(int i = l - 1; i > 0; i--){
			tab[i] = tab[i - 1];
		}
		tab[0] = temp;
	}
	
	public static int toInt(int[] tab){
		int res = 0;
		int m = 1;
		for(int i = tab.length - 1; i >= 0; i--){
			res += m * tab[i];
			m *= 10;
		}
		return res;
	}
	
	public static int[] toTab(int n){
		int[] res = new int[lint(n)];
		int temp = n;
		for(int i = res.length - 1; i >= 0; i--){
			res[i] = temp % 10;
			temp /= 10;
		}
		return res;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//String fileName = args[0];
		String fileName = "recycling/C-small-attempt0.in";
		Scanner sC = null;
		try {
			sC = new Scanner(new File(fileName));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int T = sC.nextInt();
		int A, B, l;
		
		for(int i = 0; i < T; i++){
			A = sC.nextInt();
			B = sC.nextInt();
			
			l = lint(A);
			
			int[] tab;
			
			int temp;
			
			int count = 0;
			
			for(int j = A; j < B; j++){
				tab = toTab(j);
				for(int k = 0; k < l - 1; k++){
					permut(tab);
					temp = toInt(tab);
					//System.out.println(j + " " + temp);
					if(temp > j && temp <= B){
						count++;
						//System.out.println(j + " " + temp);
					}
				}
			}
			
			System.out.println("Case #" + (i + 1) + ": " + count);
		}

	}

}
