/*
 * 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.util.ArrayList;
import java.util.Collection;

/**
 * User: avokin
 * Date: 4/14/12
 */
public class ProblemC {
    private static int[] deg = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000};

    private static int recycled(int a, int b, int n) {
        int result = 0;
        int i = 1;
        Collection<Integer> counted = new ArrayList<Integer>();
        while (i < n) {
            int a1 = a / deg[i];
            int a2 = a % deg[i];
            int a3 = a2 * deg[n - i] + a1;
            if (a < a3 && a3 <= b) {
                if (!counted.contains(a3)) {
                    result++;
                }
                counted.add(a3);
            }
            i++;
        }
        return result;
    }

    private static int solve(int a, int b) {
        int result = 0;
        int n = ("" + a).length();
        for (int i = a; i < b; i++) {
            result += recycled(i, b, n);
        }
        return result;
    }

    public static void main(String[] args) {
        String input = "50\n" +
                "173 996\n" +
                "166 983\n" +
                "130 970\n" +
                "172 382\n" +
                "15 28\n" +
                "150 943\n" +
                "10 11\n" +
                "116 976\n" +
                "100 999\n" +
                "158 918\n" +
                "142 946\n" +
                "178 963\n" +
                "147 923\n" +
                "167 965\n" +
                "28 28\n" +
                "174 362\n" +
                "100 914\n" +
                "164 982\n" +
                "104 990\n" +
                "185 990\n" +
                "114 940\n" +
                "3 4\n" +
                "147 980\n" +
                "100 999\n" +
                "104 993\n" +
                "146 967\n" +
                "138 916\n" +
                "10 10\n" +
                "163 983\n" +
                "76 99\n" +
                "917 917\n" +
                "155 994\n" +
                "562 858\n" +
                "162 933\n" +
                "164 913\n" +
                "162 947\n" +
                "113 928\n" +
                "164 911\n" +
                "140 995\n" +
                "167 927\n" +
                "137 974\n" +
                "142 953\n" +
                "156 922\n" +
                "104 999\n" +
                "162 965\n" +
                "124 962\n" +
                "175 919\n" +
                "141 960\n" +
                "168 982\n" +
                "109 920\n";

        String[] lines = input.split("\n");
        int T = Integer.parseInt(lines[0]);
        for (int i = 1; i <= T; i++) {
            String line = lines[i];
            String[] items = line.split(" ");
            int a = Integer.parseInt(items[0]);
            int b = Integer.parseInt(items[1]);
            int solution = solve(a, b);
            System.out.println("Case #" + i + ": " + solution);
        }

    }
}
