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.Scanner;


public class Q3 {
	
	public static void main(String[] args) {
		
		Scanner s = new Scanner(Q2.class.getResourceAsStream("C-large.in"));
		
		int nTests = s.nextInt();
		
		int iTests = 1;
		while (nTests-- > 0) {
			
			int resp = 0;
			
			int m = s.nextInt();
			int n = s.nextInt();
			
			//boolean[][] criv = new boolean[n-m+1][n-m+1];
			boolean[] criv = new boolean[n-m+1];
			
			for (int i = 0; i < criv.length; i++) {
				
				
				
				int current = m + i;
				String currentString = String.valueOf(current);
				
				if (currentString.charAt(0) == '0') continue;
				
				if (criv[current - m]) continue;
				
				criv[current - m] = true;
				
				int nInterv = 0;
				for (int j = 1; j < currentString.length(); j++) {
					String invString = currentString.substring(j).concat(currentString.substring(0, j));
					Integer inv = Integer.valueOf(invString);
					
					
					if (invString.charAt(0) == '0') continue;
					if (invString.equals(currentString)) continue;
					if (inv > n || inv < m) continue;
					
					if (criv[inv - m]) continue;

					criv [inv-m] = true;
					
					nInterv ++;
					
					//System.out.println(currentString + " --- " + invString);
				}
				
				if (nInterv > 0) {
					int bbb = nInterv + 1;
					resp+= factorial(bbb) / (2 * factorial(bbb-2));
				}
				
				
				
			}
			
			
			System.out.println("Case #" + iTests + ": " + resp);
			
			iTests++;
		}
		
	}
	
	public static long factorial( int n )
    {
        if( n <= 1 )     // base case
            return 1;
        else
            return n * factorial( n - 1 );
    }

}
