#include <bits/stdc++.h>
using namespace std;
const int MOD = 2520;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int b[2610], cnt = 1, size;
void bfs(int now, int sum) {
  if (now == 10) {
    if (b[sum] == 0) {
      b[sum] = cnt;
      cnt++;
    }
    return;
  }
  bfs(now + 1, sum);
  bfs(now + 1, now * sum / gcd(sum, now));
}
int a[25];
long long dp[25][60][3100];
long long dfs(int pos, int state, bool limit, int yet) {
  if (pos == 0) {
    if (yet % state == 0)
      return 1;
    else
      return 0;
  }
  int t = b[state];
  if (!limit && dp[pos][t][yet] != -1) return dp[pos][t][yet];
  int up = limit ? a[pos] : 9;
  long long ans = 0;
  for (int i = 0; i <= up; i++) {
    if (i != 0) {
      int ca = state * i / gcd(state, i);
      ans += dfs(pos - 1, ca, limit && i == up, (yet * 10 + i) % MOD);
    } else {
      ans += dfs(pos - 1, state, limit && i == up, (yet * 10 + i) % MOD);
    }
  }
  if (!limit) dp[pos][t][yet] = ans;
  return ans;
}
long long solve(long long n) {
  int cnt = 0;
  while (n) {
    cnt++;
    a[cnt] = n % 10;
    n = n / 10;
  }
  return dfs(cnt, 1, true, 0);
}
int main(void) {
  int T;
  long long l, r;
  memset(dp, -1, sizeof(dp));
  bfs(1, 1);
  cin >> T;
  while (T--) {
    cin >> l >> r;
    cout << solve(r) - solve(l - 1) << "\n";
  }
  return 0;
}
