#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); }
int a[20];
long long dp[20][2520][50];
map<int, int> mp, mp_;
long long dfs(int pos, long long sum, int sum_, bool limit) {
  if (pos == -1) {
    if (mp_[sum_] != 0)
      if (sum % mp_[sum_] == 0) return 1;
    return 0;
  }
  if (!limit && dp[pos][sum][sum_] != -1) return dp[pos][sum][sum_];
  int up = limit ? a[pos] : 9;
  long long ans = 0;
  for (int i = 0; i <= up; ++i) {
    if (i != 0 && i != 1) {
      if (mp_[sum_] % i == 0)
        ans += dfs(pos - 1, (sum * 10 + i) % 2520, sum_, limit && i == a[pos]);
      else
        ans += dfs(pos - 1, (sum * 10 + i) % 2520,
                   mp[mp_[sum_] * i / gcd(mp_[sum_], i)], limit && i == a[pos]);
    } else
      ans += dfs(pos - 1, (sum * 10 + i) % 2520, sum_, limit && i == a[pos]);
  }
  if (!limit) dp[pos][sum % 2520][sum_] = ans;
  return ans;
}
long long solve(long long x) {
  int pos = 0;
  while (x) {
    a[pos++] = x % 10;
    x /= 10;
  }
  return dfs(pos - 1, 0, 1, true);
}
int cal2(int a, int b) {
  int res = 1;
  for (int i = 1; i <= b; ++i) res *= a;
  return res;
}
int cal(int i, int j, int k, int l) {
  int res = 1;
  res = res * cal2(2, i);
  res = res * cal2(3, j);
  res = res * cal2(5, k);
  res = res * cal2(7, l);
  return res;
}
int main() {
  long long le, ri;
  int cnt = 0;
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 3; ++j) {
      for (int l = 0; l < 2; ++l) {
        for (int k = 0; k < 2; ++k) {
          int tmp = cal(i, j, k, l);
          mp[tmp] = ++cnt, mp_[cnt] = tmp;
        }
      }
    }
  }
  memset(dp, -1, sizeof(dp));
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%lld%lld", &le, &ri);
    printf("%lld\n", solve(ri) - solve(le - 1));
  }
  return 0;
}
