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.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ProblemC {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		List<Integer>[] l = genRecycled();
		
		int T = sc.nextInt();
		
		for(int i = 0; i < T; i++){
			int c = processCase(sc, l);
			
			System.out.println("Case #" + (i+1) + ": " + c);
		}
	}
	
	public static List<Integer>[] genRecycled(){
		// gen all the possible ms for a given n
		List<Integer>[] rec = new List[2000001];
		
		int cmp;
		
		// generate a load of blank lists
		for(int i = 0; i < rec.length; i++){
			// if(i % 10000 == 0) System.out.println("hi " + i);
			rec[i] = new ArrayList<Integer>();
			
			String n = Integer.toString(i);
			String m = rotate(n);
			
			// detect when back to the first
			while(!m.equals(n)){
				
				// no leading zeros
				if(m.charAt(0) != '0'){
					// find the numeric value
					cmp = Integer.parseInt(m);
					
					// number can't be greater than the max
					// and also has to be greater than n
					if(cmp < rec.length && cmp > i){
						// add it to the list of possibles
						rec[i].add(cmp);
					}
				}
				
				m = rotate(m);
			}
		}

		return rec;
	}
	
	public static String rotate(String s){
		return s.substring(1) + s.charAt(0);
	}
	
	public static int processCase(Scanner sc, List<Integer>[] l){
		int a = sc.nextInt();
		int b = sc.nextInt();
		int count = 0;
		
		// loop through the range
		for(int i = a; i < b; i++){
			List<Integer> li = l[i];
			
			// find the pre-calculated things
			for(Integer el : li){
				// if m is less than or equal to B, count it
				if(el <= b) count++;
			}
		}
		
		return count;
	}

}
