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;
	}

}

import java.io.*;
import java.security.SecureRandom;
import java.util.*;
import java.math.*;
import java.awt.geom.*;

import static java.lang.Math.*;

public class Main implements Runnable {
	
	static String filename = "C-small-attempt0";
	
	HashSet<Integer> getCycles(int n) {
		int nc = n;
		int digitcount = 0;
		int pow10 = 1;
		while (nc > 0) {
			digitcount++;
			nc /= 10;
			pow10 *= 10;
		}
		pow10 /= 10;
		HashSet<Integer> answer = new HashSet<Integer>();
		nc = n;
		for (int i = 0; i < digitcount; i++) {
			answer.add(nc);
			int lastdigit = nc % 10;
			nc /= 10;
			nc += pow10 * lastdigit;
		}
		return answer;
	}
	
	public void solve() throws Exception {
		int t = sc.nextInt();
		for (int Case = 1; Case <= t; Case++) {
			int a = sc.nextInt(), b = sc.nextInt();
			boolean[] visited = new boolean[b + 1];
			long answer = 0;
			for (int i = a; i <= b; i++) {
				if (visited[i]) {
					continue;
				}
				HashSet<Integer> cycles = getCycles(i);
				int suitable = 0;
				for (int c : cycles) {
					if (c >= a && c <= b) {
						suitable++;
						visited[c] = true;
						//out.print(c + " ");
					}
				}
				//out.println();
				answer += suitable * (suitable - 1) / 2;
			}
			//out.println();
			out.printf("Case #%d: %d\n", Case, answer);
		}
	}
	
	BufferedReader in;
	PrintWriter out;
	FastScanner sc;
	
	
	public static void main(String[] args) {
		new Thread(null, new Main(), "", 1 << 25).start();
	}
	
	@Override
	public void run() {
		try {
			init();
			solve();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			out.close();
		}
	}
	
	void init() throws Exception {
		in = new BufferedReader(new FileReader(filename + ".in"));
		out = new PrintWriter(new FileWriter(filename + ".out"));
		sc = new FastScanner(in);
	}
}

class FastScanner {
	
	BufferedReader reader;
	StringTokenizer strTok;
	
	public FastScanner(BufferedReader reader) {
		this.reader = reader;
	}
	
	public String nextToken() throws IOException {
		while (strTok == null || !strTok.hasMoreTokens()) {
			strTok = new StringTokenizer(reader.readLine());
		}
		
		return strTok.nextToken();
	}
	
	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}
	
	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}
	
	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
	
	public BigInteger nextBigInteger() throws IOException {
		return new BigInteger(nextToken());
	}
	
}
