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

package recycled;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.TreeSet;

/**
 *
 * @author Alfred
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    try {
            FileInputStream fstream = new FileInputStream("C-small-attempt0.in");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            FileWriter outFile = new FileWriter("out.txt");
            PrintWriter out = new PrintWriter(outFile);
            String line;
            line = br.readLine();
            int cases = Integer.parseInt(line);
            for (int i=1; i<=cases;i++){
                line = br.readLine();
                String[] values = line.split(" ");
                int A = Integer.parseInt(values[0]);
                int B = Integer.parseInt(values[1]);
                int total=0;
                for (int n=A; n<=B;n++){
                    TreeSet<String> solutions = new TreeSet<String>();
                    for (int k=1;k<values[1].length();k++){
                        String m = circshift(n,k);
                        int mVal = Integer.parseInt(m);
                        if(mVal<=B && mVal!=n  && mVal> n && !m.startsWith("0") && m.length()==Integer.toString(n).length() )
                        {
                            solutions.add(m);
                        }                   
                    }
                    total+=solutions.size();
                }
                out.println("Case #"+i+": "+total);
            }
            in.close();
            out.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

}

    private static String circshift(int n, int k) {
        String nString=Integer.toString(n);
        int len=nString.length();
        String m =  nString.substring(len-k)+nString.subSequence(0, len-k);
        //System.out.println(n+" "+m);
        return m;
    }

}

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

/**
 * @author Ivan Romanov
 */
public class C {

    private String solve() throws IOException {
        int a = nextInt();
        int b = nextInt();
        int ans = 0;
        for (int i = a; i < b; i++) {
            if (recycle.containsKey(i)) {
                for (int j : recycle.get(i)) {
                    if (j <= b) {
                        ans++;
                        // System.out.println(i + " " + j);
                    }
                }
            }
        }
        return "" + ans;
    }

    private Map<Integer, Set<Integer>> recycle = new HashMap<Integer, Set<Integer>>();

    private void prepare() {
        int mul = 1;
        int dig = 1;
        for (int x = 10; x <= 2000000; x++) {
            if (mul * 10 == x) {
                mul *= 10;
                dig++;
            }
            Set<Integer> recs = new HashSet<Integer>();
            int t = x;
            for (int i = 1; i < dig; i++) {
                int last = t % 10;
                t /= 10;
                t += last * mul;
                if (t > x) {
                    recs.add(t);
                }
            }
            if (!recs.isEmpty()) {
                recycle.put(x, recs);
            }
        }
    }

    private BufferedReader reader;

    private StringTokenizer tt = new StringTokenizer("");

    private String nextToken() throws IOException {
        while (!tt.hasMoreTokens()) {
            tt = new StringTokenizer(reader.readLine());
        }
        return tt.nextToken();
    }

    private int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    private void run() throws IOException {
        String problem = "C-small-attempt0";
        reader = new BufferedReader(new FileReader(problem + ".in"));
        PrintWriter writer = new PrintWriter(new File(problem + ".out"));
        try {
            prepare();
            int cases = nextInt();
            int tc = 1;
            while (tc <= cases) {
                writer.print("Case #" + tc + ": ");
                writer.println(solve());
                tc++;
            }
        } finally {
            reader.close();
            writer.close();
        }
    }

    public static void main(String[] args) {
        try {
            new C().run();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}