#include <bits/stdc++.h>
#define int long long
using namespace std;
const int p = 2520;
int T, l, r, cnt, f[20][p + 5][50], c[p + 5];
vector < int > v;
int lcm(int x, int y){if(! y)return x; return x / __gcd(x, y) * y;}
int dfs(int i, int j, int k, bool opt)
{
if(! i)return j % k ? 0 : 1;
if(! opt and f[i][j][c[k]])return f[i][j][c[k]];
int n = opt ? v[i] : 9;
int ans = 0;
for(int x = 0; x <= n; x++)ans += dfs(i - 1, (j * 10 + x) % p, lcm(k, x), opt & (x == n));
if(! opt)f[i][j][c[k]] = ans;
return ans;
}
int ans(int n)
{
v.clear(); v.push_back(1145141919810);
while(n)v.push_back(n % 10), n /= 10;
return dfs(v.size() - 1, 0, 1, 1);
}
signed main()
{
for(int i = 1; i <= p; i++)c[i] = (p % i) ? 0 : ++cnt;
scanf("%lld", &T);
while(T--)
{
int l, r;
scanf("%lld %lld", &l, &r);
printf("%lld\n", ans(r) - ans(l - 1));
}
return 0;
}