#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <map>
#define MAX 1000000000001LL
using namespace std;
queue < long long > q;
map < long long, bool > mym;
long long ans[500001], num[500001];
int len, ll;
void pre_for_lucky()
{
long long x;
len = 0;
while(!q.empty()) q.pop();
mym.clear();
q.push(4); q.push(7);
ans[++ len] = 4; ans[++ len] = 7;
while(!q.empty())
{
x = q.front();
q.pop();
if( x * 10 + 4 <= MAX && !mym[x * 10 + 4])
{
ans[++len] = x * 10 + 4;
mym[x * 10 + 4] = 1;
q.push(x * 10 + 4);
}
if( x * 10 + 7 <= MAX && !mym[x * 10 + 7])
{
ans[++len] = x * 10 + 7;
mym[x * 10 + 7] = 1;
q.push(x * 10 + 7);
}
}
}
void dfs(long long x, int index)
{
long long temp;
int i;
for(i = index; i <= len; i ++)
{
temp = MAX / x;
if(temp < ans[i]) return;
num[++ ll] = x * ans[i];
dfs(num[ll], i);
}
}
int bsearch(long long x, int inc)
{
int l = 0, r = len + 1, mid = (l + r) / 2;
if(x < ans[1]) return inc;
if(x > ans[len]) return len + inc;
while(l < r)
{
if(ans[mid] == x) return mid;
if(ans[mid + 1] == x) return mid + 1;
if(ans[mid] < x && ans[mid + 1] > x)
return mid + inc;
if(ans[mid] > x)
{
r = mid;
mid = (l + r) / 2;
}
else if(ans[mid] < x)
{
l = mid;
mid = (l + r) / 2;
}
}
}
int main()
{
freopen("in.txt","r",stdin);
int test;
long long l, r;
pre_for_lucky();
ll = 0;
dfs(1, 1);
num[0] = 0;
num[ll + 1] = MAX;
sort(num + 1, num + ll + 1);
len = 0;
for(int i = 1; i <= ll; i ++)
if(num[i] != num[i - 1])
ans[++len] = num[i];
scanf("%d", &test);
while(test --)
{
scanf("%I64d %I64d", &l, &r);
printf("%d\n",bsearch(r, 0) - bsearch(l, 1) + 1);
}
}