import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t=sc.nextInt();
		int casos=1, a, b, n, m, cont;
		while(t!=0){
			a=sc.nextInt();
			b=sc.nextInt();
			if(a>b){
				int aux=a;
				a=b;
				b=aux;
			}
			System.out.printf("Case #%d: ",casos++);
			if(a==b){
				System.out.printf("%d\n",0);
			}else{
				cont=0;
				for(n=a;n<b;n++){					
					for(m=n+1;m<=b;m++){					
						if(isRecycled(n,m)) cont++;
					}
				}
				System.out.printf("%d\n",cont);
			}
			t--;
		}		
	}
	public static boolean isRecycled(int n1, int n2){
		String s1, s2, aux;
		s1 =  String.valueOf( n1 );
		s2 =  String.valueOf( n2 );		
		boolean r = false;		
		for(int i=0;i<s1.length();i++){
			aux="";
			aux=s1.substring(i,s1.length())+s1.substring(0,i);
//			System.out.println(aux);
			if(aux.equals(s2)){
				r=true;
				break;
			}
		}		
		return r;
	}
}

package com.google.code.jam2012.problemC;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: Vahid
 * Date: 4/14/12
 * Time: 6:54 PM
 * To change this template use File | Settings | File Templates.
 */
public class ProblemC1 {

    public static void main(String[] args) throws IOException {

        Scanner scanner = new Scanner(new FileInputStream("data.in"));

        FileOutputStream out = new FileOutputStream("C1.out");
        long a, b, m,n;
        int lines = scanner.nextInt();
        scanner.nextLine();
        for (int i = 1; i<=lines; i++){
            a = scanner.nextLong();
            b = scanner.nextLong();
            out.write(("Case #"+i+": "+ calculate(a, b)).getBytes());
            out.write('\r');
            out.write('\n');
            if (scanner.hasNextLine()) scanner.nextLine();
        }
        scanner.close();
        out.close();
    }

    private static long calculate(long a, long b) {
        long logarithm = 1;
        long counter = 0;
        int len = 1;
        while (logarithm * 10 <= b) {
            logarithm *= 10;
            len++;
        }
        HashSet<Long> list;
        for (long n = a; n<b; n++) {
            list = new HashSet<Long>(100);
            long m = n;
            for (int i=1; i<len; i++){
                m = (m %10) *logarithm + (m/10);
                if ((n<m) && (m<=b) && (!list.contains(new Long(m)))) {
                    counter++;
                    list.add(new Long(m));
                }
            }
        }
        return  counter;
    }

}
