import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Q3M {
	public static void main(String[] args) throws Exception {
		compute();
	}

	private static void compute() throws Exception {
		BufferedReader br = new BufferedReader(new FileReader(new File(
				"C:\\work\\Q3\\C-small-attempt0.in")));
		String line = null;
		int i = Integer.parseInt(br.readLine());
		List l = new ArrayList();
		for (int j = 0; j < i; j++) {
			line = br.readLine();
			String[] nums = line.split(" ");
			l.add(calculate(nums));
		}
		writeOutput(l);
	}

	private static int calculate(String[] nums) {
		int min = Integer.parseInt(nums[0]);
		int max = Integer.parseInt(nums[1]);
		int count = 0;
		List l = new ArrayList();
		for (int i = min; i <= max; i++) {
			for (int times = 1; times < countDigits(i); times++) {
				int res = shiftNum(i, times);
				if (res <= max && i < res) {
					if ((!l.contains((i + ":" + res)))) {
						l.add(i + ":" + res);
						l.add(res + ":" + i);
						count++;
					}
				}
			}
		}
		return count;
	}

	private static boolean checkZeros(int temp, int res) {
		if (temp % 10 == 0 || res % 10 == 0)
			return false;
		return true;
	}

	private static int shiftNum(int n, int times) {
		int base = (int) Math.pow(10, times);
		int temp2 = n / base;
		int placeHolder = (int) Math.pow((double) 10,
				(double) countDigits(temp2));
		int res = placeHolder * (n % base) + temp2;
		if (countDigits(res) == countDigits(n)) {
			return res;
		} else {
			return 2000001;
		}
	}

	public static int countDigits(int x) {
		if (x < 10)
			return 1;
		else {
			return 1 + countDigits(x / 10);
		}
	}

	private static void writeOutput(List l) throws Exception {
		StringBuffer b = new StringBuffer();
		int i = 1;
		BufferedWriter br = new BufferedWriter(new FileWriter(new File(
				"C:\\work\\Q3\\ans.txt")));
		for (Iterator iterator = l.iterator(); iterator.hasNext();) {
			br.write("Case #" + i++ + ": " + iterator.next());
			br.newLine();
		}
		br.close();
	}

}
/**
 * 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;
   }
}
