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

public class C {

    void solve() throws IOException {
        in("C-large.in"); out("C-large.out");

        long tm = System.currentTimeMillis();
        boolean[] mask = new boolean[2000000];
        int[] r = new int[10];
        int t = readInt();
        for (int cs = 1; cs <= t; ++cs) {
            int a = readInt();
            int b = readInt();
            int ans = 0;
            for (int m = a + 1; m <= b; ++m) {
                char[] d = String.valueOf(m).toCharArray();
                int c = 0;
                for (int i = 1; i < d.length; ++i) {
                    if (d[i] != '0' && d[i] <= d[0]) {
                        int n = 0;
                        for (int j = 0; j < d.length; ++j) {
                            int jj = i + j;
                            if (jj >= d.length) jj -= d.length;
                            n = n * 10 + (d[jj] - '0');
                        }
                        if (n >= a && n < m && !mask[n]) {
                            ++ans;
                            mask[n] = true;
                            r[c++] = n;
                        }
                    }
                }
                for (int i = 0; i < c; ++i) {
                    mask[r[i]] = false;
                }
            }
            println("Case #" + cs + ": " + ans);
            //System.out.println(cs);
        }
        System.out.println("time: " + (System.currentTimeMillis() - tm));
        exit();
    }

    void in(String name) throws IOException {
        if (name.equals("__std")) {
            in = new BufferedReader(new InputStreamReader(System.in));
        } else {
            in = new BufferedReader(new FileReader(name));
        }
    }

    void out(String name) throws IOException {
        if (name.equals("__std")) {
            out = new PrintWriter(System.out);
        } else {
            out = new PrintWriter(name);
        }
    }

    void exit() {
        out.close();
        System.exit(0);
    }

    int readInt() throws IOException {
        return Integer.parseInt(readToken());
    }

    long readLong() throws IOException {
        return Long.parseLong(readToken());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readToken());
    }

    String readLine() throws IOException {
        st = null;
        return in.readLine();
    }

    String readToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    boolean eof() throws IOException {
        return !in.ready();
    }

    void print(String format, Object ... args) {
        out.println(new Formatter(Locale.US).format(format, args));
    }

    void println(String format, Object ... args) {
        out.println(new Formatter(Locale.US).format(format, args));
    }

    void print(Object value) {
        out.print(value);
    }

    void println(Object value) {
        out.println(value);
    }

    void println() {
        out.println();
    }

    StringTokenizer st;

    BufferedReader in;
    PrintWriter out;

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

import com.sun.org.apache.xerces.internal.impl.xs.opti.DefaultXMLDocumentHandler;
import util.graph.DynDjikstra;
import util.graph.Edge;
import util.graph.Node;
import util.graph.State;

import javax.xml.crypto.dsig.CanonicalizationMethod;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.TreeSet;

public class CrossingRoad extends JamProblem {

    public static void main(String[] args) throws IOException {
        CrossingRoad p = new CrossingRoad();
        p.go();
    }
    @Override
    String solveCase(JamCase jamCase) {
        CrossCase ca = (CrossCase) jamCase;
        CrossState crossState = new CrossState();
        crossState.time = 0;
        ca.startNode.last = crossState;
        CrossState state = (CrossState) DynDjikstra.FindShortest(ca.startNode, ca.stopNode);


        return "" + state.time;
    }

    @Override
    JamCase parseCase(List<String> file, int line) {
        String first = file.get(line++);
        int[] size = JamUtil.parseIntList(first, 2);
        CrossCase ca = new CrossCase();
        ca.n = size[0];
        ca.m = size[1];
        ca.lineCount = ca.n + 1;
        ca.map = new Crossing[ca.n][ca.m];
        Node[][][][] nodes = new Node[ca.n][ca.m][2][2];
        Node start = null;
        Node end = null;
        for (int y = 0; y < ca.n; y++) {
            String row = file.get(line++);
            int[] data = JamUtil.parseIntList(row, 3 * ca.m);
            for (int x = 0; x < ca.m; x++) {
                Crossing cr = new Crossing();
                ca.map[y][x] = cr;
                cr.minTime = Integer.MAX_VALUE;
                cr.s = data[3*x];
                cr.w = data[3*x+1];
                cr.t = data[3*x+2];
                cr.x = x;
                cr.y = y;
                Node ne = new CrossNode();

                Node se = new CrossNode();
                Node sw = new CrossNode();
                Node nw = new CrossNode();
                CrossStreetSNEdge snEdge1 = new CrossStreetSNEdge();
                snEdge1.s =cr.s;
                snEdge1.w = cr.w;
                snEdge1.t = cr.t;
                CrossStreetWEEdge weEdge = new CrossStreetWEEdge();
                weEdge.s= cr.s;
                weEdge.w=cr.w;
                weEdge.t=cr.t;

                snEdge1.biConnect(ne,se);
                snEdge1.biConnect(nw,sw);
                weEdge.biConnect(ne,nw);
                weEdge.biConnect(se,sw);

                Node[][] pack = nodes[y][x];
                pack[0][0] = nw;
                pack[0][1] = ne;
                pack[1][1] = se;
                pack[1][0] = sw;
                ((CrossNode)nw).label = "y " + y + " x " + x + " nw";
                ((CrossNode)ne).label = "y " + y + " x " + x + " ne";
                ((CrossNode)se).label = "y " + y + " x " + x + " se";
                ((CrossNode)sw).label = "y " + y + " x " + x + " sw";
                if (y==0 && x ==ca.m-1) {
                    ca.stopNode = pack[0][1];
                }

                if (y==ca.n-1 && x == 0) {
                    ca.startNode = pack[1][0];
                }

                if (y>0) {
                    CrossBlockEdge b1 = new CrossBlockEdge();
                    b1.biConnect(nodes[y][x][0][1],nodes[y-1][x][1][1]);
                    b1.biConnect(nodes[y][x][0][0],nodes[y-1][x][1][0]);
                }

                if (x>0) {
                    CrossBlockEdge b1 = new CrossBlockEdge();
                    b1.biConnect(nodes[y][x][0][0], nodes[y][x - 1][0][1]);
                    b1.biConnect(nodes[y][x][1][0],nodes[y][x-1][1][1]);
                }
            }
        }
        return ca;
    }
}

class CrossCase extends JamCase {
    int n, m;
    Crossing[][] map;
    public Node startNode;
    public Node stopNode;
}

class Crossing implements Comparable<Crossing>{
    int s, w, t, minTime,x,y;

    @Override
    public int compareTo(Crossing o) {
        return Integer.compare(minTime,o.minTime);
    }
}

class CrossState extends State {

    long time;
    @Override
    public int compareTo(Object o) {
        return Long.compare(time,((CrossState)o).time);
    }
}

class CrossBlockEdge extends Edge {
    @Override
    public State travel(State current) {
        CrossState old = (CrossState) current;
        CrossState newS = new CrossState();
        newS.time = old.time + 2;
        return newS;
    }
}

class CrossStreetSNEdge extends Edge {
    long s,w,t;
    @Override
    public State travel(State current) {
        CrossState old = (CrossState) current;
        CrossState newS = new CrossState();
        long adjusted = old.time - t;
        long period = s + w;
        long inCycle = adjusted % period;
        if (inCycle < s) {
            newS.time = old.time + 1;
            return newS;
        }
        newS.time = old.time + (period-inCycle);
        return newS;
    }
}

class CrossStreetWEEdge extends Edge {
    long s,w,t;
    @Override
    public State travel(State current) {
        CrossState old = (CrossState) current;
        CrossState newS = new CrossState();
        long adjusted = old.time - t;
        long period = s + w;
        long inCycle = adjusted % period;
        if (inCycle >= s) {
            newS.time = old.time + 1;
            return newS;
        }
        newS.time = old.time + (s-inCycle);
        return newS;
    }
}

class CrossNode extends Node {
    String label;
    CrossNode() {
        last = new CrossState();
    }

    @Override
    public String toString() {
        return label;
    }
}
