#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<set>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<iostream>
#include<string>
#include<cmath>
#define N 1000100
#define FOR(i,a,b) for(i=(a);i<=(b);i++)
#define ROF(i,a,b) for(i=(a);i>=(b);i--)
typedef long long LL;
using namespace std;
LL num[N],limit=1e12,Num[N],l,r;int len=0,i,j,L=0,k,T;
int find(LL x)
{
LL *p=upper_bound(Num+1,Num+1+L,x);
int pos=p-num;
return pos;
}
void dfs(int x,LL tot)
{
if (tot>1&&tot<=limit) Num[++L]=tot;
int i;
FOR(i,x,len)
{
LL tmp=tot*num[i];
if (abs(tmp)>limit) break;
dfs(i,tmp);
}
}
int getsum(LL L,LL R)
{
return find(R)-find(L-1);
}
int main()
{
FOR(i,1,12)
{
FOR(j,0,(1<<i)-1)
{
LL tot=0;
ROF(k,i-1,0)
{
if (j>>k&1) tot*=10,tot+=7;
else tot*=10,tot+=4;
}
num[++len]=tot;
}
}
dfs(1,1);
sort(Num+1,Num+1+L);
LL *ppp=unique(Num+1,Num+1+L);
L=ppp-Num-1;
scanf("%d",&T);
while (T--)
{
scanf("%lld%lld",&l,&r);
printf("%d\n",getsum(l,r));
}
}