#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T read() {
  register T sum = 0;
  register char cc = getchar();
  int sym = 1;
  while (cc != '-' && (cc > '9' || cc < '0')) cc = getchar();
  if (cc == '-') sym = -1, cc = getchar();
  sum = sum * 10 + cc - '0';
  cc = getchar();
  while (cc >= '0' && cc <= '9') sum = sum * 10 + cc - '0', cc = getchar();
  return sym * sum;
}
template <typename T>
inline T read(T& a) {
  a = read<T>();
  return a;
}
template <typename T, typename... Others>
inline void read(T& a, Others&... b) {
  a = read(a);
  read(b...);
}
int T, digit[19], vis[19][1 << 8][7][8][9];
long long f[19][1 << 8][7][8][9];
int in(int S, int x) { return S >> (x - 2) & 1; }
int add(int S, int x) {
  if (x < 2) return S;
  return S | (1 << (x - 2));
}
int check(int S, int a, int b, int c) {
  if (!S) return true;
  if (in(S, 2) && b % 2) return false;
  if (in(S, 3) && c % 3) return false;
  if (in(S, 4) && b % 4) return false;
  if (in(S, 6) && (b % 2 || c % 3)) return false;
  if (in(S, 7) && a) return false;
  if (in(S, 8) && b) return false;
  if (in(S, 9) && c) return false;
  return true;
}
long long dfs(int k, int S, int a, int b, int c, int lim) {
  if (k < 0) return check(S, a, b, c);
  if (!lim && vis[k][S][a][b][c]) return f[k][S][a][b][c];
  long long res = 0;
  int up = lim ? digit[k] : 9;
  for (int i = 0; i <= up; i++) {
    if (k == 0 && in(S, 5) && i != 0 && i != 5) continue;
    res += dfs(k - 1, add(S, i), (a * 10 + i) % 7, (b * 10 + i) % 8,
               (c * 10 + i) % 9, lim && i == up);
  }
  if (!lim) {
    f[k][S][a][b][c] = res;
    vis[k][S][a][b][c] = true;
  }
  return res;
}
long long calc(long long x) {
  for (int i = 0; i <= 18; i++) {
    digit[i] = x % 10;
    x /= 10;
  }
  return dfs(18, 0, 0, 0, 0, true);
}
int main() {
  read(T);
  for (int cas = 1; cas <= T; cas++) {
    long long l, r;
    read(l, r);
    printf("%lld\n", calc(r) - calc(l - 1));
  }
  return 0;
}
