import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;

public class Recycled {
    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int t = Integer.parseInt(br.readLine());
            for (int i = 0; i < t; i++) {
                String[] str = br.readLine().split(" ");
                int a = Integer.parseInt(str[0]);
                int b = Integer.parseInt(str[1]);
                System.out.println("Case #" + (i + 1) + ": " + find(a, b));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static int find(int a, int b) {
        HashSet<Long> used = new HashSet<Long>();
        int digits = String.valueOf(a).length();
        if (digits == 1) return 0;
        for (int i = a; i <= b; i++) {
            int s = 10;
            int m = (int) Math.pow(10, digits-1);
            for (int j = 0; j < digits-1; j++) {
                int r = i % s;
                if (r < s/10) continue;
                int q = i / s;
                int rn = r * m + q;
                s *= 10; m /= 10;
                if (i != rn && rn >= a && rn <= b) {
                    long pp;
                    if (rn < i) pp = (long)rn << 30 | i;
                    else pp = (long)i << 30 | rn;
                    if (!used.contains(pp)) {
                        used.add(pp);
                    }
                }
            }
        }
        return used.size();
    }
}

import java.io.*;
import java.util.*;
import java.math.*;
import java.text.*;

public class RecycledNumbersHard {
	
	static final int INF = 1 << 28, MAX = 1000;
	static final double EPS = 1E-9;
	
	static int digits(int x) {
		int d;
		for (d = 0; x > 0; d++)
			x /= 10;
		return d;
	}
	
	static int hash(int m, int d) {
		int pow = 1;
		for (int i = 0; i < d-1; i++)
			pow *= 10;
		
		int hash = m;
		while (d-- > 0) {
			m = m / 10 + pow * (m % 10);
			if (m < hash)
				hash = m;
		}
		
		return hash;
	}
	
	static long count(Map<Integer, Integer> counter) {
		long count = 0;
		for (int k : counter.values())
			count += k * (k-1L) / 2;
		
		return count;
	}
	
	static void solve() {
		Scanner scan = new Scanner(in);
		int T = scan.nextInt();
		for (int t = 1; t <= T; t++) {
			out.print("Case #" + t + ": ");
			int A = scan.nextInt();
			int B = scan.nextInt();
			
			long r = 0;
			int lastD = 0;
			Map<Integer, Integer> counter = new HashMap<Integer, Integer>();
			for (int x = A; x <= B; x++) {
				int d = digits(x);
				if (d != lastD) {
					lastD = d;
					r += count(counter);
					counter.clear();
				}
				
				int hash = hash(x, d);
				Integer c = counter.get(hash);
				if (c == null)
					c = 0;
				counter.put(hash, c + 1);
			}
			
			r += count(counter);
			out.println(r);
		}
	}
	
	static String read() {
		try { return in.readLine(); } catch (IOException e) { return null; }
	}
	public static void main(String[] args) throws IOException {
//		in = new BufferedReader(new InputStreamReader(System.in));
//		out = new PrintWriter(new BufferedOutputStream(System.out));
		String file = "C-large";
		in = new BufferedReader( new FileReader(file + ".in") );
		out = new PrintWriter( new FileOutputStream(file + "_" + System.currentTimeMillis() + ".out") );
		solve(); out.flush();
	}
	static BufferedReader in; static PrintWriter out;
}
