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 java.util.*;

public class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[][] s = new int[3][n];

		for (int i = 0; i < n; i++) {
			s[0][i] = sc.nextInt();
			s[1][i] = sc.nextInt();
			s[2][i] = 0;
		}

		for (int i = 0; i < n; i++) {
			List<Integer> past2 = new ArrayList<Integer>();
			List<Integer> past1 = new ArrayList<Integer>();
			if (s[0][i] < 10 && s[1][i] < 10) {
				s[2][i] = 0;
			} else {
				for (int j = s[0][i]; j < s[1][i]; j++) {
					String t1 = Integer.toString(j); // mÀ» ¹®ÀÚ¿­·Î ÀúÀå
					for (int k = 1; k < t1.length(); k++) {
						String t2 = t1.substring(k) + t1.substring(0, k); // nÀ» ¹®ÀÚ¿­·Î ÀúÀå
						if (t2.charAt(0) != '0') { // Ã¹ÀÚ¸®°¡ 0ÀÌ µÇ¾î ÀÚ¸®¼ö°¡ ³»·Á°¡´Â °æ¿ì ¿¹¿Ü Ã³¸®
							if (Integer.parseInt(t2) <= s[1][i] && Integer.parseInt(t2) >= s[0][i]) { // B ¹üÀ§¸¦ ÃÊ°úÇÏ´Â °æ¿ì ¿¹¿Ü Ã³¸®
								if (!t1.equals(t2)) { // n°ú mÀÌ °°Àº °æ¿ì ¿¹¿Ü Ã³¸®
									int same = 0;
									for(int p = 0; p < past2.size() ; p++) {
										if(past2.get(p) == Integer.parseInt(t1) && past1.get(p) == Integer.parseInt(t2)) {
											same = 1;
										}
									}
									if(same == 0) {
										s[2][i]++;
										past2.add(Integer.parseInt(t2));
										past1.add(Integer.parseInt(t1));
									}
									same = 0;
								}
							}
						}
					}
				}
			}
		}

		for (int i = 0; i < n; i++) {
			System.out.println("Case #" + (i + 1) + ": " + s[2][i]);
		}

	}
}