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 ch.socaciu.andrei.codejam;

import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Andrei Socaciu
 */
public class Recycled {

    public static void main(String[] args) throws Exception {
        String in = "data/Recycled-small-attempt0.in.txt";
        String out = "data/Recycled-small-attempt0.out.txt";
        String line = null;
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(in)));
        List<String> lines = new ArrayList<String>();
        while ((line = reader.readLine()) != null) {
            lines.add(line);
        }
        reader.close();
        int tests = Integer.parseInt(lines.get(0));
        List<String> res = new ArrayList<String>();
        int k = 1;
        for (int i = 1; i <= tests; i++) {
            line = lines.get(k++);
            String[] parts = line.split(" ");
            int a = Integer.parseInt(parts[0]);
            int b = Integer.parseInt(parts[1]);
            int result = solve(a,b);
            res.add("Case #" + i + ": " + result);
        }
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out)));
        for (String re : res) {
            writer.write(re);
            writer.write("\n");
        }
        writer.close();
    }

    private static int solve(int a, int b) {
        int count = 0;
        Set<String> unique = new HashSet<String>();
        for (int i = a; i < b; i++) {
            StringBuilder sb = new StringBuilder(Integer.toString(i));
            for (int j = 0; j < sb.length() - 1; j++) {
                char last = sb.charAt(sb.length() - 1);
                sb.deleteCharAt(sb.length() - 1);
                sb.insert(0, last);
                if (sb.charAt(0) != '0') {
                    int permutation = Integer.parseInt(sb.toString());
                    if (permutation >= a && permutation <= b && permutation > i) {
                        String stringForm = i + ", " + sb;
                        if (unique.add(stringForm)) {
                            count++;
                        }
                    }
                }
            }
        }
        return count;
    }

}
