#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
bool chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
const int maxl = 100;
const int maxx = 2520;
int tot;
int lcm[maxl], LCM[maxl][20], s[20];
long long dp[20][maxx + 100][maxl];
map<int, int> ma;
inline void file() {
  freopen("CF55D.in", "r", stdin);
  freopen("CF55D.out", "w", stdout);
}
inline void read(int &x) {
  x = 0;
  int p = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - '0');
    c = getchar();
  }
  x *= p;
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
inline void init() {
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= maxx; ++i)
    if (maxx % i == 0) {
      lcm[++tot] = i;
      ma[i] = tot;
    }
  for (int i = 1; i <= tot; ++i) {
    LCM[i][0] = i;
    for (int j = 1; j <= 9; ++j) LCM[i][j] = ma[lcm[i] * j / gcd(lcm[i], j)];
  }
  for (int j = 0; j <= 9; ++j) LCM[0][j] = j;
}
long long DFS(int k, int pres, int prel, int flag) {
  if (k == 0) {
    if (!prel) return 0;
    return pres % lcm[prel] == 0;
  }
  if (!flag && dp[k][pres][prel] != -1) return dp[k][pres][prel];
  long long ans = 0;
  int nn = flag ? s[k] : 9;
  for (int i = 0; i <= nn; ++i) {
    int nows = (10 * pres + i) % maxx;
    int nowl = LCM[prel][i];
    ans += DFS(k - 1, nows, nowl, (flag && i == nn));
  }
  if (!flag) dp[k][pres][prel] = ans;
  return ans;
}
long long calc(long long x) {
  int k = 0;
  while (x) {
    s[++k] = x % 10;
    x /= 10;
  }
  return DFS(k, 0, 0, 1);
}
int main() {
  init();
  int tt;
  scanf("%d", &tt);
  while (tt--) {
    long long L, R;
    cin >> L >> R;
    long long ans = calc(R) - calc(L - 1);
    cout << ans << endl;
  }
  return 0;
}
