#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a,b,t,d[1000006];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
freopen("SBD.inp", "r", stdin);
freopen("SBD.out", "w", stdout);
memset(d,0,sizeof d);
for(int i = 1; i <= 1000000; i++)
{
d[i] = d[i-1];
if(i%3 == 0)
{
ll t = i,uc = 1;
for(int j = 2; j <= sqrt(t) ;j++)
{
if(t%j == 0)
{
ll dem = 0;
while(t%j == 0)
{
dem++;
t/=j;
}
uc = uc*(dem+1);
}
}
if(t > 1)
uc*=2;
if(uc == 9)
d[i]++;
}
}
cin >> t;
while(t--)
{
cin >> a >> b;
cout << d[b] - d[a-1] << '\n';
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBsbCBsb25nIGxvbmcKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKbGwgYSxiLHQsZFsxMDAwMDA2XTsKaW50IG1haW4oKQp7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOyBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsKICAgIGZyZW9wZW4oIlNCRC5pbnAiLCAiciIsIHN0ZGluKTsKICAgIGZyZW9wZW4oIlNCRC5vdXQiLCAidyIsIHN0ZG91dCk7CiAgICBtZW1zZXQoZCwwLHNpemVvZiBkKTsKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gMTAwMDAwMDsgaSsrKQogICAgewogICAgICAgIGRbaV0gPSBkW2ktMV07CiAgICAgICAgaWYoaSUzID09IDApCiAgICAgICAgewogICAgICAgICAgICBsbCB0ID0gaSx1YyA9IDE7CiAgICAgICAgICAgIGZvcihpbnQgaiA9IDI7IGogPD0gc3FydCh0KSA7aisrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZih0JWogPT0gMCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBsbCBkZW0gPSAwOwogICAgICAgICAgICAgICAgICAgIHdoaWxlKHQlaiA9PSAwKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgZGVtKys7CiAgICAgICAgICAgICAgICAgICAgICAgIHQvPWo7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIHVjID0gdWMqKGRlbSsxKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZih0ID4gMSkKICAgICAgICAgICAgICAgIHVjKj0yOwogICAgICAgICAgICBpZih1YyA9PSA5KQogICAgICAgICAgICAgICAgZFtpXSsrOwogICAgICAgIH0KICAgIH0KICAgIGNpbiA+PiB0OwogICAgd2hpbGUodC0tKQogICAgewogICAgICAgIGNpbiA+PiBhID4+IGI7CiAgICAgICAgY291dCA8PCBkW2JdIC0gZFthLTFdIDw8ICdcbic7CiAgICB9Cn0K