/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package year_2012.qualification;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author paul
 * @date 14 Apr 2012
 */
public class QuestionC {

    private static Set<Integer> getCycle(int x) {

        String s = Integer.toString(x);
        Set<Integer> set = new HashSet<Integer>();
        set.add(x);
        for (int c = 1; c < s.length(); c++) {
            String t = s.substring(c).concat(s.substring(0, c));
            set.add(Integer.parseInt(t));
        }
        return set;
        
    }

    private static Set<Integer> mask(Set<Integer> set, int a, int b) {
        Set<Integer> result = new HashSet<Integer>();

        for (int x : set) {
            if (x >= a && x <= b) {
                result.add(x);
            }
        }
        
        return result;

    }


    public static void main(String[] args) throws FileNotFoundException, IOException {

        String question = "C";

//        String name = "large";
        String name = "small-attempt0";
//        String name = "test";

        String filename = String.format("%s-%s", question, name);

        BufferedReader input = new BufferedReader(
                new FileReader(String.format("/home/paul/Documents/code-jam/2012/qualification/%s.in",
                filename)));

        String firstLine = input.readLine();

        PrintWriter pw = new PrintWriter(new File(
                String.format("/home/paul/Documents/code-jam/2012/qualification/%s.out", filename)));


        int T = Integer.parseInt(firstLine);

        // Loop through test cases.
        for (int i = 0; i < T; i++) {

//             Read data.
            String[] tokens = input.readLine().split(" ");

//            int N = Integer.parseInt(input.readLine());

            int A = Integer.parseInt(tokens[0]);
            int B = Integer.parseInt(tokens[1]);

//            System.out.format("%d, %d\n", A, B);

            boolean[] used = new boolean[B+1];
            int total = 0;
            for (int n = A; n <= B; n++) {
                if (!used[n]) {
                    Set<Integer> set = mask(getCycle(n), A, B);
                    int k = set.size();
                    total += (k * (k-1))/2;
                    for (int m : set) {
                        used[m] = true;
                    }
                }
            }

            pw.format("Case #%d: %d\n", i + 1, total);
        }

        pw.close();

    }

}

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.TreeMap;

/**
 * Created with IntelliJ IDEA.
 * User: akim
 * Date: 4/14/12
 * Time: 7:45 PM
 * To change this template use File | Settings | File Templates.
 */
public class C {

    String makeHash(int a) {
        String t = Integer.toString(a);
        String ans = t;
        for (int i = 0; i < t.length(); ++i) {
            t = t.substring(1)+t.charAt(0);
            if (ans.compareTo(t) > 0) ans = t;
        }
//        System.out.println(a + " - " + ans);
        return ans;
    }

    void solve() throws FileNotFoundException {
        PrintWriter out = new PrintWriter(new FileOutputStream("output.txt"));
        Scanner in = new Scanner(new FileInputStream("input.txt"));
        int t = Integer.parseInt(in.nextLine());
        System.err.println(t);
        TreeMap<String, Long> ans = new TreeMap<String, Long>();
        for (int i = 0; i < t; ++i) {
            ans.clear();
            int A = in.nextInt();
            int B = in.nextInt();
            for (int qqq = A; qqq <= B; ++qqq) {
                String hash = makeHash(qqq);
                if (!ans.containsKey(hash)) ans.put(hash, 0L);
                long tmp = ans.get(hash);
                ans.put(hash, tmp + 1);
            }
            long rrr = 0;
            /*for (int a = A; a <= B; ++a)
                for (int b = a + 1; b <= B; ++b)
                    if (makeHash(a).equals(makeHash(b))) --rrr;*/
            for (String h : ans.keySet()) {
                long tmp = ans.get(h);
                rrr += tmp * (tmp - 1) / 2;
            }
            out.println("Case #" + (i + 1) + ": " + rrr);
            System.err.println((i + 1) + " " + rrr);
        }
        out.flush();
        out.close();
    }


    public static void main(String[] args) throws FileNotFoundException {
        new C().solve();
    }
}
