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 ru.bobukh.problems;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

/**
 *
 * @author anton.bobukh
 */
public class ProblemC_RecycledNumbers {
    
    public static class Pair {
        public final int n;
        public final int m;
        
        public Pair(int n, int m) {
            this.n = n;
            this.m = m;       
        }
    }
    
    public static class PairComparator implements Comparator<Pair> {

        @Override
        public int compare(Pair o1, Pair o2) {
            if(o1.m - o2.m != 0)
                return o1.m - o2.m;
            else
                return o1.n - o2.n;
        }
        
    }
    
    public static void main(String[] args) throws IOException {
        
        try (Scanner input = new Scanner(new BufferedReader(new FileReader("C:\\Users\\maggot\\Desktop\\C-large.in")))) {
            int T = input.nextInt();
            input.nextLine();
            
            try(PrintWriter output = new PrintWriter("C:\\Users\\maggot\\Desktop\\output.txt")) {
                for(int k = 0; k < T; ++k) {
                    Set<Pair> pairs = new TreeSet<>(new PairComparator());
                    int A = input.nextInt();
                    int B = input.nextInt();
                    
                    int numberOfDigits = 1;
                    int value = A;
                    while((value = value / 10) > 0)
                        ++numberOfDigits;
                    
                    for(int n = A; n <= B; ++n) {
                        for(int power = 0; power < numberOfDigits; ++power) {
                            int m = n % (int)Math.pow(10, power + 1) * (int)Math.pow(10, numberOfDigits - power - 1) + n / (int)Math.pow(10, power + 1);
                            if(n < m && m <= B)
                                pairs.add(new Pair(n, m));
                        }
                    }
                    
                    output.println(String.format("Case #%d: %d", k + 1, pairs.size()));
                }
            }
        }
    }
    
}
