package fixjava;

public class StringUtils {

	/** Repeat the given string the requested number of times. */
	public static String repeat(String str, int numTimes) {
		StringBuilder buf = new StringBuilder(Math.max(0, str.length() * numTimes));
		for (int i = 0; i < numTimes; i++)
			buf.append(str);
		return buf.toString();
	}

	/**
	 * Pad the left hand side of a field with the given character. Always adds at least one pad character. If the length of str is
	 * greater than numPlaces-1, then the output string will be longer than numPlaces.
	 */
	public static String padLeft(String str, char padChar, int numPlaces) {
		int bufSize = Math.max(numPlaces, str.length() + 1);
		StringBuilder buf = new StringBuilder(Math.max(numPlaces, str.length() + 1));
		buf.append(padChar);
		for (int i = 1, mi = bufSize - str.length(); i < mi; i++)
			buf.append(padChar);
		buf.append(str);
		return buf.toString();
	}

	/**
	 * Pad the right hand side of a field with the given character. Always adds at least one pad character. If the length of str is
	 * greater than numPlaces-1, then the output string will be longer than numPlaces.
	 */
	public static String padRight(String str, char padChar, int numPlaces) {
		int bufSize = Math.max(numPlaces, str.length() + 1);
		StringBuilder buf = new StringBuilder(Math.max(numPlaces, str.length() + 1));
		buf.append(str);
		buf.append(padChar);
		while (buf.length() < bufSize)
			buf.append(padChar);
		return buf.toString();
	}

	/** Intern all strings in an array, skipping null elements. */
	public static String[] intern(String[] arr) {
		for (int i = 0; i < arr.length; i++)
			arr[i] = arr[i].intern();
		return arr;
	}

	/** Intern a string, ignoring null */
	public static String intern(String str) {
		return str == null ? null : str.intern();
	}

}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;


public class C {

	/**
	 * @param args
	 */
	static String n;
	static int count = 0;
	static String h;
	static int t ;
	static ArrayList<Integer> memo  = new ArrayList<Integer>();
	static ArrayList<Integer> old  = new ArrayList<Integer>();
	
	public static int solve(int s, int e){
		count = 0 ;
		memo.clear();
		old.clear();
		
		for (int i = s; i <= e; i++) {
			n = i+"";
			in:for (int j = 0; j < n.length(); j++) {
				
				h = n.substring(n.length()-j) + n.substring(0,n.length()-j);
				t = Integer.parseInt(h);
				
				if (t <= e && t>i ) {
					for (int k = 0; k < memo.size(); k++) {
						if (memo.get(k)==t && old.get(k)==i)
							continue in;
					}
					memo.add(t);
					old.add(i);
					count++;
				}
			}
			
		}		
		return count;
	}
	
	
	static BitSet v1  = new BitSet(2000000);
	static int [] index = new int[2000000];
	
	public static int solve2(int s, int e){
		count = 0;
		v1.clear();
		Arrays.fill(index, 0,0,e);

		for (int i = s; i <= e; i++) {
			n = i+"";
			for (int j = 0; j <= n.length(); j++) {
				
				h = n.substring(n.length()-j) + n.substring(0,n.length()-j);
				t = Integer.parseInt(h);

				if (t <= e && t >i ) {
						if (v1.get(i) && index[i]==t)
							continue;
						count++;
						v1.set(i);
						index[i] = t ;
				}
			}
		}
		
		return count;
	}
	
	
	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new FileReader("c.in"));
		BufferedWriter bw = new BufferedWriter(new FileWriter("c.out"));
		int n;
		int t = Integer.parseInt(br.readLine().trim());
		String ss[];
		
		for (int i = 1; i <= t; i++) {
		ss= br.readLine().split(" ");
		n = solve2(Integer.parseInt(ss[0]), Integer.parseInt(ss[1]));
//		System.out.println("Case #"+i+": "+n);
		bw.write("Case #"+i+": "+n);
		bw.newLine();
		}
		
	bw.close();
	}
}
