#include <bits/stdc++.h>
using namespace std;
const int MOD = 2520;
long long dp[30][2525][50];
int num[20];
int has[3000];
int s[3000];
int gcd(int a, int b) {
  if (!b)
    return a;
  else
    return gcd(b, a % b);
}
int getlcm(int a, int b) {
  int d = gcd(a, b);
  return (a / d) * b;
}
long long dfs(int pos, int mod, int lcm, int limit) {
  if (pos < 0) return mod % lcm == 0;
  if (!limit && dp[pos][mod][has[lcm]] != -1) return dp[pos][mod][has[lcm]];
  int tlcm;
  int endi = 9;
  if (limit) endi = num[pos];
  long long res = 0;
  for (int i = 0; i <= endi; i++) {
    if (!i)
      tlcm = lcm;
    else
      tlcm = getlcm(i, lcm);
    res += dfs(pos - 1, (mod * 10 + i) % MOD, tlcm, limit && (i == endi));
  }
  if (!limit) dp[pos][mod][has[lcm]] = res;
  return res;
}
long long solve(long long n) {
  int cnt = 0;
  while (n > 0) {
    num[cnt++] = n % 10;
    n /= 10;
  }
  return dfs(cnt - 1, 0, 1, 1);
}
void init() {
  memset(has, 0, sizeof(has));
  int l = 0;
  for (int i = 0; i < (1 << 9); i++) {
    int now = 1;
    for (int j = 1; j < 9; j++) {
      if (i & (1 << j)) {
        now = getlcm(now, j + 1);
      }
    }
    has[now] = 1;
  }
  for (int i = 0; i <= MOD; i++) {
    if (has[i]) {
      s[l] = i;
      has[i] = l++;
    }
  }
}
int main() {
  memset(dp, -1, sizeof(dp));
  int t;
  cin >> t;
  init();
  while (t--) {
    long long l, r;
    cin >> l >> r;
    long long ans1 = solve(l - 1);
    long long ans2 = solve(r);
    cout << ans2 - ans1 << endl;
  }
  return 0;
}
