import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Recycled {
	public static void main(String[] args) throws Exception{
		String inputFile = "C-small-attempt0.in";
		
		BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));
		BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output")));
		long num_cases = Long.parseLong(input.readLine());
		int current_case = 0;
		
		while (current_case < num_cases){
			current_case++;
			
			String[] fields = input.readLine().split(" ");
			int A = Integer.parseInt(fields[0]);
			int B = Integer.parseInt(fields[1]);
			int total = 0;
			
			for (int n = A; n < B; n++){
				for (int m = n+1; m <= B; m++){
					if (isRecycled(n,m)) total++;
				}
			}
			
			System.out.println("Case #" + current_case + ": " + total);
			output.write("Case #" + current_case + ": " + total + "\n");
		}
		output.close();
	}

	private static boolean isRecycled(int n, int m) {
		String sn = ""+n;
		String sm = ""+m;
		if (sn.length() != sm.length()) return false;
		int totaln = 0, totalm = 0;
		for (int i = 0; i < sn.length(); i++){
			totaln += sn.charAt(i);
			totalm += sm.charAt(i);
		}
		if (totaln != totalm) return false;
		for (int i = 0; i < sn.length()-1; i++){
			sm = sm.charAt(sm.length() - 1) + sm.substring(0, sm.length() - 1);
			if (sm.equals(sn)) return true;
		}
		return false;
	}
}

/**
 * RecycledNumbers.java
 *
 * Either use command line or first argument must be path of input file.
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * @author bsanghvi
 */
public class RecycledNumbers {

   private static final Map<Integer, Long> PRODUCT_SUM_MAP = new HashMap<Integer, Long>();
   private static final Map<Integer, boolean[]> CONTAINS_DIGIT_MAP = new HashMap<Integer, boolean[]>();
   private static final Map<Integer, List<Integer>> DIGITS_MAP = new HashMap<Integer, List<Integer>>();

   /**
    * The main method.
    * 
    * @param args the arguments
    */
   public static void main(final String[] args) {
      try {
         final Scanner in;
         if (args.length >= 1) {
            in = new Scanner(new File(args[0]));
         } else {
            in = new Scanner(System.in);
         }

         final int T = in.nextInt();

         for (int test = 1; test <= T; test++) {
            final long result = processCase(in);
            System.out.println("Case #" + test + ": " + result);
         }
      } catch (final FileNotFoundException e) {
         e.printStackTrace();
      }
   }

   private static long processCase(final Scanner in) {
      long result = 0;

      final int A = in.nextInt();
      final int B = in.nextInt();

      for (int a = A; a < B; ++a) {
         final boolean[] aContainsDigit;
         final List<Integer> aDigits;
         final long aProductSum;
         if (PRODUCT_SUM_MAP.containsKey(a)) {
            assert (CONTAINS_DIGIT_MAP.containsKey(a));
            assert (DIGITS_MAP.containsKey(a));

            aContainsDigit = CONTAINS_DIGIT_MAP.get(a);
            aDigits = DIGITS_MAP.get(a);
            aProductSum = PRODUCT_SUM_MAP.get(a);
         } else {
            aContainsDigit = new boolean[10];
            aDigits = new ArrayList<Integer>();
            aProductSum = processNumber(a, aDigits, aContainsDigit);
         }

         for (int b = a + 1; b <= B; ++b) {
            final boolean[] bContainsDigit;
            final List<Integer> bDigits;
            final long bProductSum;
            if (PRODUCT_SUM_MAP.containsKey(b)) {
               assert (CONTAINS_DIGIT_MAP.containsKey(b));
               assert (DIGITS_MAP.containsKey(b));

               bContainsDigit = CONTAINS_DIGIT_MAP.get(b);
               bDigits = DIGITS_MAP.get(b);
               bProductSum = PRODUCT_SUM_MAP.get(b);
            } else {
               bContainsDigit = new boolean[10];
               bDigits = new ArrayList<Integer>();
               bProductSum = processNumber(b, bDigits, bContainsDigit);
            }

            if (aProductSum == bProductSum && Arrays.equals(aContainsDigit, bContainsDigit)
                  && cyclicEquals(aDigits, bDigits)) {
               ++result;
            }
         }
      }

      return result;
   }

   private static long processNumber(int n, final List<Integer> digits, final boolean[] digitContained) {
      assert (digitContained.length == 10);
      assert (digits.isEmpty());

      Arrays.fill(digitContained, false);
      int sum = 0;
      long product = 1;

      do {
         final int digit = n % 10;
         digits.add(digit);
         digitContained[digit] = true;
         sum += digit;
         if (digit != 0) {
            product *= digit;
         }
         n /= 10;
      } while (n > 0);

      Collections.reverse(digits);

      DIGITS_MAP.put(n, digits);
      CONTAINS_DIGIT_MAP.put(n, digitContained);
      final long productSum = product + sum;
      PRODUCT_SUM_MAP.put(n, productSum);

      return productSum;
   }

   private static boolean cyclicEquals(final List<Integer> a, final List<Integer> b) {
      if (a != null && b != null) {
         final int size = a.size();
         if (size == b.size()) {
            final int a0 = a.get(0);
            for (int i = 0; i < size; ++i) {
               if (a0 == b.get(i)) {
                  if (cyclicEquals(a, b, i)) {
                     return true;
                  }
               }
            }
         }
      }
      return false;
   }

   private static boolean cyclicEquals(final List<Integer> a, final List<Integer> b, final int bStartPosition) {
      assert (a != null);
      assert (b != null);
      final int size = a.size();
      assert (size == b.size());

      for (int i = 0, j = bStartPosition; i < size; ++i, ++j) {
         if (j >= size) {
            j = 0;
         }
         if (a.get(i) != b.get(j)) {
            return false;
         }
      }
      return true;
   }
}
