import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;

public class Recycled {
    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int t = Integer.parseInt(br.readLine());
            for (int i = 0; i < t; i++) {
                String[] str = br.readLine().split(" ");
                int a = Integer.parseInt(str[0]);
                int b = Integer.parseInt(str[1]);
                System.out.println("Case #" + (i + 1) + ": " + find(a, b));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static int find(int a, int b) {
        HashSet<Long> used = new HashSet<Long>();
        int digits = String.valueOf(a).length();
        if (digits == 1) return 0;
        for (int i = a; i <= b; i++) {
            int s = 10;
            int m = (int) Math.pow(10, digits-1);
            for (int j = 0; j < digits-1; j++) {
                int r = i % s;
                if (r < s/10) continue;
                int q = i / s;
                int rn = r * m + q;
                s *= 10; m /= 10;
                if (i != rn && rn >= a && rn <= b) {
                    long pp;
                    if (rn < i) pp = (long)rn << 30 | i;
                    else pp = (long)i << 30 | rn;
                    if (!used.contains(pp)) {
                        used.add(pp);
                    }
                }
            }
        }
        return used.size();
    }
}

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

public class CodeJam {
    public static void main(String[] args) {
        try {
            //recycleLarge(1000000, 2000000);
            // read
            FileInputStream instream = new FileInputStream("A-small.in");
            DataInputStream in = new DataInputStream(instream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            // write
            FileOutputStream outstream = new FileOutputStream("A-small.out");
            DataOutputStream out = new DataOutputStream(outstream);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));

            // reading file information
            int testcases = Integer.parseInt(br.readLine());
            for (int i = 0; i < testcases; i++) {
                // -------------------------------------------------------------------------------------------------------------
                String line = br.readLine();
                String[] items = line.split(" ");
                int A = Integer.parseInt(items[0]);
                int B = Integer.parseInt(items[1]);
                // -------------------------------------------------------------------------------------------------------------
                bw.write("Case #" + (i + 1) + ": " + recycleLarge(A, B) + "\r\n");
            }

            // close input file
            in.close();
            // close output file
            bw.flush();
            out.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }

    private static int eval(int i) {
        if (i < 0)
            return 0;
        else
            return i;
    }

    private static void printArray(Object[] array) {
        for (Object ch : array)
            System.out.print(ch);
    }

    private static boolean isRecycled(String str, String m) {
        for (int i = 1; i < str.length(); i++) {
            String digits = str.substring(i) + str.substring(0, i);
            if (digits.equals(m)) return true;
        }
        return false;
    }

    private static String convertTo(int n) {
        String str = "";
        while (n != 0) {
            str = (n % 10) + str;
            n = n / 10;
        }
        return str;
    }

    private static long recycle(int A, int B) {
        long count = 0;
        for (int n = A; n < B; n++) {
            for (int m = n + 1; m <= B; m++) {
                String nstr = convertTo(n);
                String mstr = convertTo(m);
                if (isRecycled(nstr, mstr))
                    count++;
            }
        }
        return count;
    }

    private static long evaluate(int n, int A, int B) {
        String nstr = convertTo(n);
        int c = 0;
        for (int i = 1; i < nstr.length(); i++) {
            String digits = nstr.substring(i) + nstr.substring(0, i);
            int variation = Integer.parseInt(digits);
            if(digits.charAt(0) == '0') continue;
            if (digits.equals(nstr)) continue;
            if (variation <= B && variation >= A){
                visit[variation] = true;
                c++;
            }

        }
        return sum(c);
    }

    private static long sum(int n){
        return n * (n+1) / 2;
    }

    private static long recycleLarge(int A, int B) {
        long sum = 0;
        visit = new boolean[B+1];
        for(int i=0; i<B; i++) visit[i] = false;
        for (int n = A; n <= B; n++) {
            if (visit[n] == false)
                sum += evaluate(n, A, B);
        }
        return sum;
    }

    private static boolean[] visit = null;


}

