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

using namespace std;





bool mark[100005];



void sieve()

{

    int sz = 100000;

    mark[1] = true;

    mark[0] = true;



    for (int i=4; i<=sz; i+=2)    mark[i] = true;



    for (int p=3; p<=sz; p+=2)

    {

        if (mark[p] == false)

        {

            for (int i = 2*p; i<=sz; i += p)

            {

                mark[i] = true;

            }

        }

    }

}



int cnt[100005];



int main(){



    memset(mark, 0, sizeof(mark));



    sieve();



    memset(cnt, 0, sizeof(cnt));



    for(int i = 1; i<=100005; i++){



        int x = i;

        int y = (i+1)/2;



        if(mark[x] == false && mark[y] == false) cnt[i] = 1;

        else cnt[i] = 0;

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







    }



    int q;



    scanf("%d", &q);



    while(q--){



        int x, y;



        scanf("%d %d", &x, &y);



        printf("%d\n", cnt[y] - cnt[x-1]);





    }



    return 0;





    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.24034429762675x):
#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;

}