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;
	}
}

import util.CombinationGenerator;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SetUtil {
    public static long getOne(int n) {
        return 1<<n;
    }

    public static List<Integer> getList(long set, int n) {
        ArrayList<Integer> integers = new ArrayList<>();
        for (int i=0;i<n; i++) {
            if ((set & getOne(i))!=0) {
                integers.add(i);
            }
        }
        return integers;
    }
    public static <T>long getForList(Map<T,Integer> positions, List<T> objs) {
        long res = 0L;
        for (T obj : objs) {
            int pos = positions.get(obj);
            res |= getOne(pos);
        }
        return res;
    }

    public static void cycleSubsets(int num, SubsetCallBack callBack) {
        callBack.run(0, 0);
        for (int i =  1; i<=num; i++) {
            CombinationGenerator generator = new CombinationGenerator(num,i);

            while (generator.hasMore()) {
                long n = 0;
                int[] next = generator.getNext();
                for (int i1 : next) {
                    n |= getOne(i1);
                }
                callBack.run(n,i);
            }
        }

    }

    public static interface SubsetCallBack {
        void run(long subset, int i);
    }
}
