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

public class recycled_numbers {
	public static void main(String[]a) throws Exception{
		
		Scanner f = new Scanner(new File(a[0]));
		int result=0;
		int A,B;
		String n,m;
		HashSet<String> s=new HashSet<String>();
		
		int T=f.nextInt();	//T total case count
		for (int t=1; t<=T;t++){//for each case t
			
			s.clear();
			result=0;				//reset
			A=f.nextInt();B=f.nextInt();	//get values for next case
			
			if (A==B)result=0;//remove case not meeting problem limits 
			else{
				for(int i=A;i<=B;i++){//for each int in range
					n=Integer.toString(i);
					m=new String(n);
					//System.out.println(n);
					for (int j=1;j<n.length();j++){//move each digit to the front & test
						m = m.substring(m.length()-1)+m.substring(0,m.length()-1);
						if(m.matches("^0+[\\d]+")!=true &&
								Integer.parseInt(m)<=B &&
								Integer.parseInt(n)<Integer.parseInt(m)){
							s.add(new String(n+","+m));
							//result++;
							//System.out.println(" matched: "+m);
						}
						
					}
				}
				result = s.size();
			}

			//display output
			System.out.println("Case #" + t + ": " + result);
		}
	}
}
package blacky.codejam.qualifications;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class RecycledNumbers {

	private static class Pair {
		
		final public int m;
		
		final public int n;
		
		public Pair(int m, int n) {
			this.m = m;
			this.n = n;
		}
		
		public boolean equals(Object other) {
			return ((Pair)other).m == this.m && ((Pair)other).n == this.n;
		}
		
		public String toString() {
			return String.format("(%d,%d)", this.m, this.n);
		}
		
		public int hashCode() {
			return this.m * 31 + n;
		}
		
	}
	
	public static void main(String args[]) throws Exception {
		BufferedReader reader = new BufferedReader(new FileReader(new File(args[0])));
		int numberOfTestCases = Integer.parseInt(reader.readLine());
		for(int i = 0; i < numberOfTestCases; i++) {
			String numbers[] = reader.readLine().split(" ");
			Set<Pair> pairs = new HashSet<Pair>();
			int a = Integer.parseInt(numbers[0]);
			int b = Integer.parseInt(numbers[1]);
			for(int j = a; j < b; j++) {
				String jAsString = Integer.toString(j);
				int recycledNumbers[] = new int[jAsString.length()];
				recycledNumbers[0] = j;
				for(int k = 1; k < jAsString.length(); k++) {
					int offset = jAsString.length() - k;
					String currentRecNum = jAsString.substring(offset).concat(jAsString.substring(0, offset));
					recycledNumbers[k] = Integer.parseInt(currentRecNum);
				}
				Arrays.sort(recycledNumbers);
				for(int k = 0; k < recycledNumbers.length - 1; k++) {
					if(!(a <= recycledNumbers[k] && recycledNumbers[k] <= b)) continue;
					for(int l = k + 1; l < recycledNumbers.length; l++) {
						if(!(a <= recycledNumbers[l] && recycledNumbers[l] <= b)) continue;
						if(recycledNumbers[l] == recycledNumbers[k]) continue;
						pairs.add(new Pair(recycledNumbers[k], recycledNumbers[l]));
					}
				}
			}
			System.out.println(String.format("Case #%d: %d", i + 1, pairs.size()));
		}
	}
	
}
