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 java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

public class ProblemC2 
{
	public boolean checked[] = new boolean[2000000];
	public int min, max;
	
	public static void main(String [] args)
	{
		new ProblemC2();
	}
	
	public ProblemC2()
	{
		try
		{
			String [] inputFiles = { "C-large" };//,"A-large" };
			BufferedReader in;
			PrintWriter out;
			for(int i = 0 ; i < inputFiles.length; i++)
			{
				in = new BufferedReader(new FileReader(inputFiles[i]));
				out = new PrintWriter(new FileWriter(inputFiles[i]+".out"),true);
				
				int numCases = Integer.parseInt(in.readLine());
				String [] items = null;
				for(int j = 0 ; j < numCases; j++)
				{
					checked = new boolean[2000000];
					items = in.readLine().split("\\s+");
					min = Integer.parseInt(items[0]);
					max = Integer.parseInt(items[1]);
					int total = 0;
					for(int x = min; x <= max; x++)
					{
						if(! checked[x])
							total+=checkRecycled(x);
					}
					out.println("Case #" + (j+1) + ": " + total);
				}
				in.close();
				out.close();
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	private int checkRecycled(int x) 
	{
		int matches=1;
		checked[x]=true;
		if(x<10)
			return 0;
		
		int numChecks = 0;
		if(x>=1000000)
		{
			numChecks = 6;
		}
		else if(x>=100000)
		{
			numChecks = 5;
		}
		else if(x>=10000)
		{
			numChecks = 4;
		}
		else if(x>=1000)
		{
			numChecks = 3;
		}
		else if(x>=100)
		{
			numChecks = 2;
		}
		else
		{
			numChecks = 1;
		}
		
		String xstr=new String(""+x);
		for(int i=0; i<numChecks ; i++)
		{
			xstr=xstr.substring(xstr.length()-1)+""+xstr.substring(0,xstr.length()-1);
			if(xstr.startsWith("0"))
				continue;
			int tmp = Integer.parseInt(xstr);
			if(tmp<=2000000)
				checked[tmp]=true;
			if(x != tmp && tmp >= min && tmp <= max)
				matches++;
		}
		if(matches == 1)
			return 0;
		else if(matches == 2)
			return 1;
		else if(matches == 3)
			return 3;
		else if(matches == 4)
			return 6;
		else if(matches == 5)
			return 10;
		else if(matches == 6)
			return 15;
		else if(matches == 7)
			return 21;
		
		return 0;
	}

}
