/*
 * 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.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

public class Puzzle3 {

    public static String parseString(int q, String A) {
        String tmp = A.substring(q+1, A.length()) + A.substring(0,q+1);
        return tmp;
    }
    
    public static boolean isReclycled(String A, String B) {
        for(int i=0; i<A.length(); i++) {
            if(parseString(i, A).equals(B)) {
                return true;
            }
        }
        return false;
    }

    //O(n*m)
    public static int countRecyclesBetween(int A, int B) {
        int count = 0;
        for (int i=A; i< B; i++) {
            for(int j = i+1; j<= B; j++) {
                if(i == j ) continue;
                if(isReclycled(String.valueOf(i), String.valueOf(j))) {
                    count++;
                }
            }
        }
        
        return count;
    }


    private static String executeTestCase(String input) {

        String strOutput = "";
        BufferedReader in = new BufferedReader(new StringReader(input));
        int testCases = 0;
        try {
            testCases = Integer.valueOf(in.readLine());
        } catch (IOException e) {
            return "Invalid test case input at line 1";
        }

        try {
            if (testCases > 0) {
                for (int i = 1; i <= testCases; i++) {
                    String lines[] = in.readLine().split(" ");
                    int n = Integer.valueOf(lines[0]);
                    int m = Integer.valueOf(lines[1]);
                    strOutput+="Case #" + i + ": " + countRecyclesBetween(n,m) + "\n";
                }
            }
        } catch (Exception e) {
            return "Invalid test case input";
        }

        return strOutput.toString();

    }

    public static void main(String[] args) {

        String input = "50\n" +
                "3 4\n" +
                "156 971\n" +
                "302 482\n" +
                "163 919\n" +
                "148 941\n" +
                "19 59\n" +
                "109 959\n" +
                "140 945\n" +
                "155 963\n" +
                "137 975\n" +
                "172 928\n" +
                "12 37\n" +
                "123 933\n" +
                "113 925\n" +
                "444 756\n" +
                "10 11\n" +
                "112 977\n" +
                "143 938\n" +
                "149 997\n" +
                "158 938\n" +
                "104 957\n" +
                "117 928\n" +
                "159 967\n" +
                "171 951\n" +
                "128 921\n" +
                "676 676\n" +
                "440 440\n" +
                "125 939\n" +
                "116 949\n" +
                "100 100\n" +
                "10 99\n" +
                "117 950\n" +
                "128 972\n" +
                "158 938\n" +
                "178 941\n" +
                "143 975\n" +
                "100 999\n" +
                "956 969\n" +
                "176 938\n" +
                "124 966\n" +
                "181 967\n" +
                "190 956\n" +
                "145 940\n" +
                "187 994\n" +
                "167 996\n" +
                "167 921\n" +
                "135 916\n" +
                "156 926\n" +
                "170 994\n" +
                "103 955\n";
        

        System.out.println(executeTestCase(input));

    }
}
