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.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class RecycledNumbersCodeJam2012Q {

	public long  solves(String A,String B){
		long a = Long.parseLong(A);
		long b = Long.parseLong(B);
		long s = 0 ; 
		for(long i = a ;i <= b; i++){
			s+= possibilities( Long.toString(i), a, b);
		}
		return s;
	}
	public long possibilities(String A,long a , long b ){
		long s = 0 ;
		long a0 = Long.parseLong(A);
		List<String> dup = new ArrayList<String>();
		dup.add(A);
		for(int i = 1 ; i < A.length();i++){
			String n= A.substring(i);
			String n1= A.substring(0,i);
			String aa = new String(n+n1);
			//System.out.println(aa);
			
			long a1= Long.parseLong(aa);
			
			if ( a1 < a0 &&  a1>= a && a1<=b && !dup.contains(aa)){	
				++s;
				dup.add(aa);
			}
		}
		return s ;
	}
	public static void main(String[] args) throws IOException {
		RecycledNumbersCodeJam2012Q si = new RecycledNumbersCodeJam2012Q();
	
		int T ;
		String CurLine = ""; 
	        
		InputStreamReader converter = new InputStreamReader(System.in);
		BufferedReader in = new BufferedReader(converter);
		CurLine = in.readLine();
		T= Integer.parseInt(CurLine);
		
		for(int i = 0 ; i < T;i++) {
			CurLine = in.readLine();
			String A = CurLine.split(" ")[0];
			String B = CurLine.split(" ")[1];
			System.out.println("Case #"+ (i+1)+": "+si.solves(A,B));
			
		}
	}
	
}
