#include <bits/stdc++.h>
using namespace std;
const int mod = 2520;
int lenl, lenr;
int L[25], R[25];
long long l, r;
long long dp[25][50][2530];
map<int, int> mp;
void get(long long x, int &l, int a[]) {
  l = 0;
  while (x) {
    a[++l] = x % 10;
    x /= 10;
  }
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int cnt = 0;
long long calc(int a[], int pos, int num, long long lcm, int pa, int oa) {
  if (pos == 0) return num % lcm == 0;
  if (pa && dp[pos][mp[lcm]][num] != -1) return dp[pos][mp[lcm]][num];
  long long res = 0;
  for (int i = 0, up = pa ? 9 : a[pos]; i <= up; i++) {
    int tmp = (num * 10 + i) % mod;
    long long g = gcd(lcm, i);
    if (i != 0)
      res += calc(a, pos - 1, tmp, lcm / g * i, pa | (i < a[pos]), oa | i);
    else
      res += calc(a, pos - 1, tmp, lcm, pa | (i < a[pos]), oa | i);
  }
  if (pa) dp[pos][mp[lcm]][num] = res;
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int i, j, t = 0;
  for (i = 1; i <= mod; i++)
    if (mod % i == 0) mp[i] = ++t;
  memset(dp, -1, sizeof(dp));
  cin >> t;
  while (t--) {
    cin >> l >> r;
    if (l > r) swap(l, r);
    get(l - 1, lenl, L), get(r, lenr, R);
    cout << calc(R, lenr, 0, 1, 0, 0) - calc(L, lenl, 0, 1, 0, 0) << endl;
  }
  return 0;
}
