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 algorithm.adhoc;

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

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 4/15/12
 * Time: 1:00 AM
 * To change this template use File | Settings | File Templates.
 */
public class GoogleCodeJam {
    public static void main(String[] arg) throws IOException {
        Scanner scanner = new Scanner(new FileReader("G:\\C-small-attempt1.in"));
        PrintWriter pw = new PrintWriter("output.txt");
        int t, tcase = 1;
        int a, b, start, end, temp;
        int count = 0;
        t = scanner.nextInt();
        String tempStr, tempStr2;
        char[] chars;
        char tmp, temp1;
        while (t > 0) {
            count = 0;
            a = scanner.nextInt();
            b = scanner.nextInt();
            start = a;
            end = b;
            for (int i = start; i <= end; ++i) {
                tempStr = Integer.toString(i);
                if (tempStr.length() != 1) {
                    if (tempStr.length() == 2) {
                        chars = tempStr.toCharArray();
                        tmp = chars[0];
                        chars[0] = chars[1];
                        chars[1] = tmp;
                        tempStr2 = new String(chars);
                        temp = Integer.parseInt(tempStr2);
                        if (temp > i && temp <= b) ++count;
                    } else if (tempStr.length() == 3) {
                        chars = tempStr.toCharArray();
                        tmp = chars[0];
                        chars[0] = chars[2];
                        chars[2] = tmp;
                        tmp = chars[1];
                        chars[1] = chars[2];
                        chars[2] = tmp;
                        tempStr2 = new String(chars);
                        temp = Integer.parseInt(tempStr2);
                        if (temp > i && temp <= b) ++count;
                        chars = tempStr.toCharArray();
                        tmp = chars[0];
                        chars[0] = chars[1];
                        chars[1] = tmp;
                        tmp = chars[1];
                        chars[1] = chars[2];
                        chars[2] = tmp;
                        tempStr2 = new String(chars);
                        temp = Integer.parseInt(tempStr2);
                        if (temp > i && temp <= b) ++count;
                    }
                }
            }

            //System.out.println(count);
            pw.println("Case #" + tcase + ": " + count);
            --t;
            ++tcase;
        }
        pw.close();
        scanner.close();
    }
}
