#include <bits/stdc++.h>
using namespace std;
const int N = 20;
long long dp[N][50][2600];
int a[N];
int hhash[2600];
const long long mod = 2520;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long dfs(int pos, int presum, int prelcm, bool limit) {
  if (pos == -1) {
    if (presum % prelcm == 0) {
      return 1;
    }
    return 0;
  }
  if (!limit && dp[pos][hhash[prelcm]][presum] != -1)
    return dp[pos][hhash[prelcm]][presum];
  int up = limit ? a[pos] : 9;
  long long ans = 0;
  for (int i = 0; i <= up; i++) {
    long long sum = (presum * 10 + i) % mod;
    long long l = prelcm;
    if (i) {
      l = lcm(prelcm, i);
    }
    ans += dfs(pos - 1, sum, l, limit && i == a[pos]);
  }
  if (!limit) dp[pos][hhash[prelcm]][presum] = ans;
  return ans;
}
long long f(long long x) {
  int i = 0;
  while (x) {
    a[i++] = x % 10;
    x /= 10;
  }
  return dfs(i - 1, 0, 1, 1);
}
int main() {
  int tt;
  scanf("%d", &tt);
  int p = 0;
  for (int i = 1; i <= 2520; i++) {
    if (2520 % i == 0) hhash[i] = p++;
  }
  long long l, r;
  memset(dp, -1, sizeof(dp));
  while (tt--) {
    scanf("%I64d%I64d", &l, &r);
    if (l > r) swap(l, r);
    long long ans1 = f(r);
    long long ans2 = f(l - 1);
    printf("%I64d\n", ans1 - ans2);
  }
}
