#include <iostream>
#include <math.h>
using namespace std;
#define MAX 1000000
int snt[MAX+10];
int mcd[MAX+10];
int sont(int n)
{
if (n<2)
return 0;
for (int i=2; i<=sqrt(n); i++)
{
if (n%i==0)
{
return 0;
}
}
return 1;
}
void init()
{
for (int i=1; i<=MAX; i++)
{
snt[i] = 1;
mcd[i] = 0;
}
}
void sangnt()
{
snt[0] = 0;
snt[1] = 0;
for (int i=2; i<=sqrt(MAX); i++)
{
if (snt[i] == 1)
{
for (int j=2; j<=MAX/i; j++)
{
snt[i*j] = 0;
}
}
}
}
int main()
{
init();
sangnt();
int t;
cin>>t;
while(t--)
{
int a, b;
cin>>a>>b;
int ts = 0;
for (int i=a; i<=b; i++)
{
if (snt[i]==1 && snt[i+6]==1 && i+6<=b)
{
ts ++;
}
}
cout<<ts<<endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWF0aC5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBNQVggMTAwMDAwMAppbnQgc250W01BWCsxMF07CmludCBtY2RbTUFYKzEwXTsKCmludCBzb250KGludCBuKQp7CglpZiAobjwyKQoJCXJldHVybiAwOwoJZm9yIChpbnQgaT0yOyBpPD1zcXJ0KG4pOyBpKyspCgl7CgkJaWYgKG4laT09MCkKCQl7CgkJCXJldHVybiAwOwoJCX0KCX0KCXJldHVybiAxOwp9Cgp2b2lkIGluaXQoKQp7Cglmb3IgKGludCBpPTE7IGk8PU1BWDsgaSsrKQoJewoJCXNudFtpXSA9IDE7CgkJbWNkW2ldID0gMDsKCX0KfQoKdm9pZCBzYW5nbnQoKQp7CglzbnRbMF0gPSAwOwoJc250WzFdID0gMDsKCWZvciAoaW50IGk9MjsgaTw9c3FydChNQVgpOyBpKyspCgl7CgkJaWYgKHNudFtpXSA9PSAxKQoJCXsKCQkJZm9yIChpbnQgaj0yOyBqPD1NQVgvaTsgaisrKQoJCQl7CgkJCQlzbnRbaSpqXSA9IDA7CgkJ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