#include <bits/stdc++.h>
int const maxl = 22;
int const maxm = 2520;
long long f[maxl][maxm + 4][50][2];
int a[maxl];
std::vector<int> alcm;
long long plcm[maxm + 4][10];
long long kth[maxm + 4];
long long l, r;
int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }
int lcm(int x, int y) {
  if (!x) return y;
  if (!y) return x;
  return x * y / gcd(x, y);
}
void init() {
  for (int i2 = 0, t2 = 1; i2 < 4; i2++, t2 *= 2)
    for (int i3 = 0, t3 = 1; i3 < 3; i3++, t3 *= 3)
      for (int i5 = 0, t5 = 1; i5 < 2; i5++, t5 *= 5)
        for (int i7 = 0, t7 = 1; i7 < 2; i7++, t7 *= 7)
          alcm.push_back(t2 * t3 * t5 * t7);
  std::sort(alcm.begin(), alcm.end());
  for (int i = 0; i <= maxm; i++)
    for (int j = 0; j < 10; j++) plcm[i][j] = lcm(i, j);
  for (int i = 0; i < (int)alcm.size(); i++) kth[alcm[i]] = i;
}
int get_len(long long x) {
  int ret = 0;
  for (; x; x /= 10) a[ret++] = x % 10;
  return ret;
}
long long calc(long long x) {
  int n = get_len(x);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j < maxm; j++)
      for (int k = 0; k < (int)alcm.size(); k++)
        for (int l = 0; l < 2; l++) f[i][j][k][l] = 0;
  f[0][0][0][0] = 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < maxm; j++)
      for (int k = 0; k < (int)alcm.size(); k++)
        for (int l = 0; l < 2; l++) {
          if (!f[i][j][k][l]) continue;
          int up = (l ? 9 : a[n - i - 1]);
          for (int ti = 0; ti <= up; ti++) {
            int last = l || (ti < a[n - i - 1]);
            f[i + 1][(j * 10 + ti) % maxm][kth[plcm[alcm[k]][ti]]][last] +=
                f[i][j][k][l];
          }
        }
  long long ans = 0;
  for (int i = 0; i < maxm; i++)
    for (int k = 0; k < (int)alcm.size(); k++)
      for (int l = 0; l < 2; l++)
        if (!(i % alcm[k])) ans += f[n][i][k][l];
  return ans;
}
int main() {
  std::ios::sync_with_stdio(false);
  init();
  int T;
  std::cin >> T;
  while (T--) {
    std::cin >> l >> r;
    std::cout << calc(r) - calc(l - 1) << '\n';
  }
}
