#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll A[19], cnt, dp[19][4][3][2][2][504][2];
const int p3[]{1, 3, 9};
ll dfs(int pos, int level2, int level3, int level5, int level7, int sum,
       bool limit) {
  ll ans = 0;
  if (dp[pos][level2][level3][level5][level7][sum][limit] != -1)
    return dp[pos][level2][level3][level5][level7][sum][limit];
  for (int v = 0; v <= (limit ? A[pos] : 9); ++v) {
    int l2 = level2, l3 = level3, l5 = level5, l7 = level7;
    int cur = (sum * 10 + v) % 504;
    if (v == 2) l2 = max(l2, 1);
    if (v == 3) l3 = max(l3, 1);
    if (v == 4) l2 = max(l2, 2);
    if (v == 5) l5 = 1;
    if (v == 6) l2 = max(l2, 1), l3 = max(l3, 1);
    if (v == 7) l7 = 1;
    if (v == 8) l2 = 3;
    if (v == 9) l3 = 2;
    if (pos == cnt - 1)
      ans += cur % (1 << l2) == 0 && cur % p3[l3] == 0 &&
             cur % (l7 ? 7 : 1) == 0 && (!l5 || v == 5 || v == 0);
    else
      ans += dfs(pos + 1, l2, l3, l5, l7, cur, limit && v == A[pos]);
  }
  if (!limit) dp[pos][level2][level3][level5][level7][sum][limit] = ans;
  return ans;
}
ll f(ll x) {
  if (x == 0) return 1;
  cnt = 0;
  while (x) A[cnt++] = x % 10, x /= 10;
  reverse(A, A + cnt);
  memset(dp, -1, sizeof(dp));
  return dfs(0, 0, 0, 0, 0, 0, true);
}
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    ll x, y;
    cin >> x >> y;
    ll l = f(x - 1), r = f(y);
    cout << r - l << "\n";
  }
  return 0;
}
