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.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.*;

/**
 * @author Seitaro Sakoda
 *
 */
public class RecycledNumbers {
	
	private static final HashMap<Long, Integer> cache = new HashMap<Long, Integer>();
	private static final ArrayList<Long> list = new ArrayList<Long>();
	private static final long MASK = 10000000;
	
	private static void init(){
		for(int i=10;i<=2000000;i++){
			int L = (int)Math.log10(i);
			int D = (int)Math.pow(10.0, L);
			int k = i;
			for(int j=0;j<L;j++){
				k = (k%D)*10+(k/D);
				if(k!=i) {
					long min = Math.min(i, k);
					long max = Math.max(i,k);
					long key = min*MASK + max;
					if(!cache.containsKey(key)){
						cache.put(key, 0);
					}
				}
			}
		}
		list.addAll(cache.keySet());
	}

	
	public static void main(String[] args) throws Exception {
		DataReader dr = new DataReader();
		int NoT = dr.readInt();
		ArrayList<String> ans = new ArrayList<String>();
		long start = System.currentTimeMillis();
		init();
		System.out.println("INIT:"+(System.currentTimeMillis()-start)+" ms");
		for(int not=0;not<NoT;not++){
			int[] tmp = dr.readInts();
			long start1 = System.currentTimeMillis();
			ans.add("Case #"+(not+1)+": "+doit(tmp[0],tmp[1]));
			System.out.println((not+1)+":"+(System.currentTimeMillis()-start1)+" ms");	
		}
		dr.close();
		DataWriter dw = new DataWriter();
		for(int not=0;not<NoT;not++) dw.writeLine(ans.get(not));
		dw.close();
	}

	/**
	 * @param c
	 * @param data 523
	 * @return
	 */
	private static int doit(int A, int B) {
		int ans = 0;
		for(long a : list){
			if((int)(a/MASK)>=A && (int)(a%MASK)<=B) ans++;
		}
		return ans;
	}
	
	
	
	
	
	
	static class DataReader {
		public BufferedReader br;
		
		public DataReader(String filename) throws Exception {
			br = new BufferedReader(new FileReader(filename));
		}
		
		public DataReader() throws Exception {
			this("data.in");
		}
		
		private String readLine() throws Exception {
			return br.readLine();
		}
		
		public int readInt() throws Exception {
			return Integer.parseInt(readLine());
		}
		
		public int[] readInts() throws Exception {
			String[] tmp = readLine().split(" ");
			int[] ret = new int[tmp.length];
			for(int i=0;i<tmp.length;i++) ret[i] = Integer.parseInt(tmp[i]);
			return ret;
		}
		
		public String readString() throws Exception {
			return readLine();
		}
		
		public String[] readStrings() throws Exception {
			return readLine().split(" ");
		}
		
		public void close() throws Exception {
			br.close();
		}	
	}
		
	static class DataWriter {
		public BufferedWriter bw;
		
		public DataWriter(String filename) throws Exception {
			bw = new BufferedWriter(new FileWriter(filename));
		}
		
		public DataWriter() throws Exception {
			this("data.out");
		}
		
		public void writeLine(String line) throws Exception {
			bw.write(line);
			bw.newLine();
		}
		
		public void close() throws Exception {
			bw.flush();
			bw.close();
		}
		
	}

}
