// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



#define ll      long long

#define MAX     100007

#define inf     99999999999999

#define MOD     1000000007

#define pii     pair<int,int>

#define FIN     freopen("input.txt","r",stdin);

#define FOUT    freopen("output.txt","w",stdout);



int nprime;

bool mark[MAX];



void sieve (void)

{

    long long i,j,lim=sqrt(MAX*1.0)+2;

    mark[0]=mark[1]=1;

    for(i=4;i<MAX;i+=2)

        mark[i]=1;

    for(i=3;i<=lim;i+=2)

    {

        if(!mark[i])

        {

            for(j=i*i;j<=MAX;j+=2*i)

                mark[j]=1;

        }

    }

}

ll q,l,r,res[MAX];

void pre()

{

    for(ll i=1;i<MAX;i+=2)

    {

        if(!mark[i] && !mark[(i+1)>>1])

        {

            res[i]++;

        }

    }

    for(ll i=1;i<MAX;i++)

    {

        res[i] += res[i-1];

    }

}

int main ()

{

    sieve();

    pre();

    scanf("%lld",&q);

    while(q--)

    {

        scanf("%lld %lld",&l,&r);

        printf("%lld\n",res[r]-res[l-1]);

    }

}




// Faster C++ solution for the same problem (measured speedup: 1.2017141002811775x):
#include <bits/stdc++.h>



using namespace std;



#define sq(a) ((a) * (a))

#define all(x) (x).begin(), (x).end()

#define pb push_back

#define mp(a, b) make_pair(a, b)

#define what_is(x) cerr << fixed << setprecision(2) << #x << " = " << x << endl

#define setbitj(n, j) (n |= (1 << (j)))

#define isbitjOn(n, j) (((n & (1 << (j))) > 0) ? 1 : 0)

#define safeMod(a, m) ((((a)%(m))+(m))%(m))/*handling negative sign of remainder*/



typedef long long ll;

typedef unsigned long long ull;

typedef pair<int, int> pii;

typedef vector<int> vi;

typedef vector<pii> vii;



const double EPS = 1e-9;

const int INF = 0x3f3f3f3f;

const double PI = acos(-1.0);

const int MAX = 100005;

const int MOD = 1000000007;



inline bool isEq(double a, double b){return abs(a - b) < EPS;}

inline double toRad(double deg){return deg*PI/180;}

inline double toDeg(double rad){return rad*180/PI;}



inline int geti(int &_int){return scanf("%d", &_int);}

inline int getii(int &_int, int &_int2){return scanf("%d %d", &_int, &_int2);}

inline int getiii(int &_int, int &_int2, int &_int3){return scanf("%d %d %d", &_int, &_int2, &_int3);}



inline int getl(ll &_ll){return scanf("%lld", &_ll);}

inline int getll(ll &_ll, ll &_ll2){return scanf("%lld %lld", &_ll, &_ll2);}

inline int getlll(ll &_ll, ll &_ll2, ll &_ll3){return scanf("%lld %lld %lld", &_ll, &_ll2, &_ll3);}



inline int getd(double &_double){return scanf("%lf", &_double);}

inline int getdd(double &_double, double &_double2){return scanf("%lf %lf", &_double, &_double2);}



inline int getstr(char *str){return scanf("%s", str);}



//#define isValid(a, b) (a >= 0 && a < b)

//int dr[]  =  {0, -1, -1, -1,  0,  1, 1, 1};

//int dc[]  =  {1,  1,  0, -1, -1, -1, 0, 1};

bool status[MAX];

vector<int>primes;



void sieve()

{

    status[1] = 1;

    primes.pb(2);

    for(int i = 3; i*i < MAX; i++){

        if(!status[i]){

            primes.pb(i);

            //what_is(i*i);

            for(ll j = (ll)i*i; j < MAX; j+=i+i)

                status[j] = 1;

        }

    }

}



int cnt[MAX];



int main(int argc, char* argv[])

{

    //freopen("in.txt", "r", stdin);

    //freopen("out.txt", "w", stdout);

    ios_base::sync_with_stdio(false); cin.tie(NULL);

    sieve();

    cnt[3]=1;

    for(int i = 4; i < MAX; i++){

        cnt[i] = cnt[i-1];

        if((i%2 ==1 && ((i+1)/2)%2==1 && !status[i] && !status[(i+1)/2]) || i == 2017)

           cnt[i]++;

    }

    int q, l, r;

    cin >> q;

    while(q--){

        cin >> l >> r;

        cout << cnt[r] - cnt[l-1] << endl;

    }

    return 0;

}