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();
    }
}

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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;

/**
 *
 * @author PARIMAL
 */
public class RecycledNumbers {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        String inputFile = "E:\\C-large.in";
        String outputFile = "E:\\C-large.out";

        File f = new File(inputFile);
        FileInputStream fis = null;
        BufferedReader reader = null;

        try {
            fis = new FileInputStream(f);
            reader = new BufferedReader(new InputStreamReader(fis));
            String line = reader.readLine();
            //ret=line.split(" ");

            int loopCount = Integer.parseInt(line);

            int i;
            int a, b;
            long A, B;
            int RecycledPairs[] = new int[loopCount];
            for (int j = 0; j < RecycledPairs.length; j++) {
                RecycledPairs[j] = 0;
            }
            //System.out.println("loop"+loopCount);
            String out[] = new String[loopCount];
            String bounds[] = new String[2];

            for (i = 0; i < loopCount; i++) {

                String t = reader.readLine();
                bounds = t.split(" ");
                A = Long.parseLong(bounds[0]);
                B = Long.parseLong(bounds[1]);

                int lengthA = bounds[0].length();

                if (lengthA == 1) {
                    RecycledPairs[i] = 0;
                    out[i] = String.valueOf(RecycledPairs[i]);
                } else {
                    String tempA, lastA, firstA, finalA;
                    long finalAns;

                    //for (int j = a; j <= b; j++) {
                        //init
                        long jL=A;
                        while(jL<=B){
                        tempA = String.valueOf(jL);
                        //System.out.println("tempA " + tempA);


                        for (int k = 1; k < lengthA; k++) {
                            lastA = tempA.substring(lengthA - k);    //12345 ==> 45

                            //System.out.println("lastA "+lastA);
                            //System.out.println("firstA "+firstA);

                            if (Long.parseLong(lastA) == 0) {
                                //System.out.println("Encountered zeros with " + k + " times");
                            } else {

                                firstA = tempA.substring(0, lengthA - k);

                                finalA = lastA + firstA;

                                finalAns = Long.parseLong(finalA);
                                if (jL < finalAns && finalAns <=B) {
                                    RecycledPairs[i]++;
                                    //System.out.println("Re: " + RecycledPairs[i]);
                                }
                            }

                        }
                        out[i] = String.valueOf(RecycledPairs[i]);
                        jL++;
                    }
                }

            }



            for (int j = 0; j < loopCount; j++) {
                String o = " ";
                StringBuffer sB = new StringBuffer(100);
                sB.append("Case #" + (j + 1) + ": ");

                o=sB.append(out[j]).toString();

                out[j] = o;
            }



            FileOutputStream fileOutputStream = null;
            try {
                File fOut = new File(outputFile);
                fileOutputStream = new FileOutputStream(fOut);

                for (int m = 0; m < out.length; m++) {

                    out[m] += "\n";
                    byte[] bytes = out[m].getBytes();
                    fileOutputStream.write(bytes);
                }


                fileOutputStream.flush();
                fileOutputStream.close();

                fis.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
