/*
 * 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 static java.util.Arrays.deepToString;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class Solution {
	private static void debug(final Object...objs) {
		System.out.println(deepToString(objs));
	}

	int rotate(int n, int d) {
		int f = n / d;
		return (n - f * d) * 10 + f;
	}

	class Pair{
		Set<Integer> elems = new HashSet<Integer>();
		Pair(final int first, final int second) {
			elems.add(first);
			elems.add(second);
		}
		@Override
		public int hashCode() {
			return elems.hashCode();
		}
		@Override
		public boolean equals(final Object obj) {
			if (this == obj) return true;
			if (!(obj instanceof Pair)) return false;
			final Pair other = (Pair) obj;
			return elems.equals(other.elems);
		}
	}


	private String solveCase() throws IOException {
		final int A = nextInt();
		final int B = nextInt();
		int d = 1;
		int N = 1;
		while (d * 10 <= A) {
			d *= 10;
			N++;
		}
		int res = 0;
		Set<Pair> all = new HashSet<Pair>();
		for (int j = A; j <= B; j++) {
			Set<Integer> ns = new HashSet<Integer>();
			int x = rotate(j, d);
			for (int i = 0; i < N; i++) {
				if (A <= x && x <= B) ns.add(x);
				x = rotate(x, d);
			}
			int[] as = new int[ns.size()];
			int i = 0;
			for (Integer it : ns) {
				as[i++] = it;
			}
			for (int m = 0; m < as.length - 1; m++) {
				for (int n = m + 1; n < as.length; n++) {
					all.add(new Pair(as[m], as[n]));
				}
			}
		}
		return String.valueOf(all.size());
	}

	private void solve() throws IOException {
		final int T = nextInt();
		for (int test = 0; test < T; test++) {
			final String result = "Case #" + (test + 1) + ": " + solveCase();
			pw.println(result);
			System.out.println(result);
		}
	}

	private BufferedReader br;
	private PrintWriter pw;
	private StringTokenizer st;

	int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

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

	public static void main(final String[] args) throws IOException {
		new Solution().run();
	}

	private void run() throws IOException {
		br = new BufferedReader(new FileReader("input.txt"));
		pw = new PrintWriter("output.txt");
		solve();
		br.close();
		pw.flush();
		pw.close();
	}
}