import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class Recycle {

	private static HashMap<Integer, HashSet<Integer>> map = new HashMap<Integer, HashSet<Integer>>();
	private static HashSet<Integer> toSkip = new HashSet<Integer>();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int T = reader.nextInt();
		for (int tc = 1; tc <= T; tc++) {
			int a = reader.nextInt();
			int b = reader.nextInt();
			long before = System.currentTimeMillis();
			int res = doit(a, b);
			System.out.printf("Case #%d: %d\n", tc, res);
			long after = System.currentTimeMillis();
//			System.out.println("time taken: " + (after - before));
		}
	}

	private static int doit(int a, int b) {
		int count = 0;
		HashSet<Integer> skip = new HashSet<Integer>(toSkip);
		for (int i = a; i <= b; i++) {
			if (skip.contains(i))
				continue;
			HashSet<Integer> arr = generate(i);
//			if(arr.size() > 1) {
//				map.put(i, arr);
//			}
			// System.out.println(i + " --> " +
			// Arrays.deepToString(arr.toArray()));
			int temp = 0;
			if (arr.size() > 1) {
				for (int x : arr) {
					if (x >= a && x <= b) {
						temp++;
						skip.add(x);
					}
				}
				count += temp * (temp - 1) / 2;
			}
//			System.out.println("not skipping " + i + " gen: " + arr.size()+ " " + (temp * (temp - 1) / 2));
		}
		return count;
	}

	private static HashSet<Integer> generate(int num) {
		if(map.containsKey(num)) {
			HashSet<Integer> list = map.get(num);
			return list;
		}
		HashSet<Integer> list = new HashSet<Integer>();
		String s = "" + num;
		list.add(num);
		int len = s.length();
		for (int i = 1; i < len; i++) {
			String temp = s.substring(i) + s.substring(0, i);
			// System.out.println("temp: " + temp);
			if (temp.charAt(0) == '0')
				continue;
			int x = Integer.parseInt(temp);
			if( x <= 2000000) {
				list.add(x);
			}
		}
		if(list.size() > 1) {
			map.put(num, list);
		} else {
			toSkip.add(num);
		}
		return list;
	}

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.falidae;


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

/**
 *
 * @author LittleRock
 */
public class RecycledNumbers {

    public RecycledNumbers() {
    }

    public void init() {

    }
    
    public long solve(int a, int b) {
        Set<Integer> valid = new HashSet<Integer>();
        long reval = 0l;
        for (int i = a; i <= b; i++) {
            String digits = Integer.toString(i);
            char last = digits.charAt(0);
            boolean allSame = true;
            for (int j = 1; j < digits.length(); j++) {
                if (digits.charAt(j) != last) {
                    allSame = false;
                }
            }
            if (allSame) {
                continue;
            }
            int pow10a = 1;
            int pow10b = (int)Math.pow(10, digits.length());
            valid.clear();
            
            for (int j = 1; j < digits.length(); j++) {
                pow10a *= 10;
                pow10b /= 10;
                int front = i / pow10a;
                int back = i % pow10a;
                int shifted = back * pow10b + front;
//                System.out.println("front " + front + ", back " + back);
//                System.out.println("shifted " + shifted);
                if (Integer.toString(shifted).length() == digits.length() && shifted <= b && shifted > i) {
                    valid.add(shifted);
                }
            }
//            if (valid.size() > 0) {
//                System.out.println(i + " cycle valid " + valid.size());
//            }
            
            reval += valid.size();
        }
        return reval;
    }

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(new FileReader("in.txt"));
        PrintWriter out = new PrintWriter(new FileWriter("out.txt"));
        RecycledNumbers solver = new RecycledNumbers();

        int testCount = in.nextInt();
        for (int i = 0; i < testCount; i++) {
            solver.init();
            printIndex(i, out);
            out.print(solver.solve(in.nextInt(), in.nextInt()));
            out.println();
        }

        in.close();
        out.flush();
        out.close();
    }

    private static void printIndex(int i, PrintWriter out) {
        out.printf("Case #%d: ", i + 1);
    }
}