/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package recyclednumbers;

import java.util.HashSet;

/**
 *
 * @author vandit
 */
public class RecycleNumbers {

    private HashSet<String> numbers = new HashSet<String>();
    private HashSet<String> initialTempNumbers = new HashSet<String>();
    private HashSet<String> finalTempNumbers = new HashSet<String>();

    HashSet<Pair> numberPairs = new HashSet<Pair>();


    private void findRecycledNumbers(int start, int end, int places) {


        for (int i = start; i <= end; i++) {
            String initialNumber = Integer.toString(i);
            //if (!(numbers.contains(initialNumber) || finalTempNumbers.contains(initialNumber))) {

                StringBuffer tempNumber = new StringBuffer(initialNumber);
                int len = tempNumber.length();
                int startIndexToMove = len - places;
                String tempString = tempNumber.substring(startIndexToMove);
                if ((places == 1 && tempString.equals("0")) || tempString.charAt(0) == '0') {
                    continue;
                }
                tempNumber.delete(startIndexToMove, len);
                String finalTempNumber = tempString + tempNumber.toString();

                if (! (finalTempNumber.equals(initialNumber) || Integer.parseInt(finalTempNumber) > end || Integer.parseInt(finalTempNumber) < Integer.parseInt(initialNumber))) {
                    numbers.add(initialNumber);
                    finalTempNumbers.add(finalTempNumber);
                    Pair pair = new Pair(finalTempNumber,initialNumber);
                    numberPairs.add(pair);
                }
            }

        

    }

    public HashSet<Pair> findAllRecycledNumbers(int start, int end) {
        int length = Integer.toString(start).length();
        for (int i = 1; i < length; i++) {
            findRecycledNumbers(start, end, i);
        }
        return numberPairs;
    }
}

import java.util.*;

public class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[][] s = new int[3][n];

		for (int i = 0; i < n; i++) {
			s[0][i] = sc.nextInt();
			s[1][i] = sc.nextInt();
			s[2][i] = 0;
		}

		for (int i = 0; i < n; i++) {
			List<Integer> past2 = new ArrayList<Integer>();
			List<Integer> past1 = new ArrayList<Integer>();
			if (s[0][i] < 10 && s[1][i] < 10) {
				s[2][i] = 0;
			} else {
				for (int j = s[0][i]; j < s[1][i]; j++) {
					String t1 = Integer.toString(j); // mÀ» ¹®ÀÚ¿­·Î ÀúÀå
					for (int k = 1; k < t1.length(); k++) {
						String t2 = t1.substring(k) + t1.substring(0, k); // nÀ» ¹®ÀÚ¿­·Î ÀúÀå
						if (t2.charAt(0) != '0') { // Ã¹ÀÚ¸®°¡ 0ÀÌ µÇ¾î ÀÚ¸®¼ö°¡ ³»·Á°¡´Â °æ¿ì ¿¹¿Ü Ã³¸®
							if (Integer.parseInt(t2) <= s[1][i] && Integer.parseInt(t2) >= s[0][i]) { // B ¹üÀ§¸¦ ÃÊ°úÇÏ´Â °æ¿ì ¿¹¿Ü Ã³¸®
								if (!t1.equals(t2)) { // n°ú mÀÌ °°Àº °æ¿ì ¿¹¿Ü Ã³¸®
									int same = 0;
									for(int p = 0; p < past2.size() ; p++) {
										if(past2.get(p) == Integer.parseInt(t1) && past1.get(p) == Integer.parseInt(t2)) {
											same = 1;
										}
									}
									if(same == 0) {
										s[2][i]++;
										past2.add(Integer.parseInt(t2));
										past1.add(Integer.parseInt(t1));
									}
									same = 0;
								}
							}
						}
					}
				}
			}
		}

		for (int i = 0; i < n; i++) {
			System.out.println("Case #" + (i + 1) + ": " + s[2][i]);
		}

	}
}