// Original C++ solution (slower):
#include <iostream>

#include <map>

#include <vector>

#include <algorithm>

#include <string>

#include <queue>

#include <iomanip>

#include <set>

#include <unordered_map>



#include <math.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>



using namespace std;



#define rep(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)

#define reps(i,n) for(int i=1,i##_len=(n);i<=i##_len;++i)

#define rrep(i,n) for(int i=((int)((n)-1));i>=0;--i)

#define rreps(i,n) for(int i=((int)(n));i>0;--i)

#define rept(n) for(int ___i___=0,i##_len=(n);___i___<i##_len;++___i___)

#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)

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

#define SZ(x) ((int)(x).size())

#define ZERO(a) memset(a,0,sizeof(a))

#define BIT(n) (1LL<<(n))

#define UNIQUE(v) v.erase(unique(v.begin(),v.end()),v.end());

#define CEIL(x, y) (((x) + (y) - 1) / (y))



// vector

template <typename T>

istream &operator>>(istream &is, vector<T> &vec) {

    for (T &x : vec) is >> x;

    return is;

}

// pair

template <typename T, typename U>

ostream &operator<<(ostream &os, pair<T, U> &pair_var) {

    os << "(" << pair_var.first << ", " << pair_var.second << ")";

    return os;

}

// vector

template <typename T>

ostream &operator<<(ostream &os, const vector<T> &vec) {

    os << "{";

    for (int i = 0; i < vec.size(); i++) {

        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");

    }

    os << "}";

    return os;

}

// map

template <typename T, typename U>

ostream &operator<<(ostream &os, map<T, U> &map_var) {

    os << "{";

    repi(itr, map_var) {

        os << *itr;

        itr++;

        if (itr != map_var.end()) os << ", ";

        itr--;

    }

    os << "}";

    return os;

}

// set

template <typename T>

ostream &operator<<(ostream &os, set<T> &set_var) {

    os << "{";

    repi(itr, set_var) {

        os << *itr;

        itr++;

        if (itr != set_var.end()) os << ", ";

        itr--;

    }

    os << "}";

    return os;

}

#define DUMPOUT cout

void dump_func() {

    DUMPOUT << endl;

}

template <class Head, class... Tail>

void dump_func(Head &&head, Tail &&... tail) {

    DUMPOUT << head;

    if (sizeof...(Tail) > 0) {

        DUMPOUT << ", ";

    }

    dump_func(std::move(tail)...);

}

#ifdef LOCAL_

#define dump(...)                                                              \

    DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \

            << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \

            << endl                                                            \

            << "    ",                                                         \

        dump_func(__VA_ARGS__)

#else

#define dump(...)

#endif



typedef long long int lli;

typedef pair<int, int> ii;

typedef priority_queue<int, vector<int>, greater<int> > heapq;

int dx[4]={1,0,-1,0};

int dy[4]={0,1,0,-1};

struct aaa{

    aaa(){

        cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);

    };

}aaaaaaa;



/**

 * 素数判定

 * @order: O(N^1/2)

 **/

bool is_prime(int n) {

    for (int i = 2; i * i <= n; i++) {

        if (n % i == 0) return false;

    }

    return n != 1;

}

/**

 * エラトステネスの篩

 * @param: n [2, n) の素数

 * @order: O(NloglogN)

 **/

vector<int> sieve(int n) {

    vector<int> ret;

    vector<int> is_prime_arr(n, 1);

    for (int i = 2; i < n; i++) {

        if (is_prime_arr[i]) {

            ret.push_back(i);

            for (int j = 2 * i; j <= n; j += i) is_prime_arr[j] = 0;

        }

    }

    return ret;

}

// #define int long long int

signed main() {

    int Q; cin >> Q;

    vector<int> prime = sieve(100000);

    int likes[100001]; ZERO(likes);

    for(auto&& p: prime) {

        if (p != 2 && is_prime((p + 1) / 2)) {

            likes[p]++;

        }

    }

    reps(i, 100000) {

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

    }



    rept(Q) {

        int l, r; cin >> l >> r;

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

    }

    return 0;

}


// Optimized solution to the same problem (faster):
/**

 * Includes

 */

#include <iostream>

#include <map>

#include <vector>

#include <algorithm>

#include <string>

#include <stack>

#include <queue>

#include <iomanip>

#include <set>

#include <unordered_map>



#include <math.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>



using namespace std;



/**

 * Macros

 */

#define rep(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)

#define repx(i,s,e,d) for(int i=s,i##_end=(e);i<i##_end;i+=d)

#define repxr(i,s,e,d) for(int i=s,i##_end=(e);i>i##_end;i+=d)

#define rept(n) for(int ___i___=0,i##_len=(n);___i___<i##_len;++___i___)

#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)

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

#define SZ(x) ((int)(x).size())

#define ZERO(a) memset(a,0,sizeof(a))

#define BIT(n) (1LL<<(n))

#define UNIQUE(v) v.erase(unique(v.begin(),v.end()),v.end());

#define CEIL(x, y) (((x) + ((x) > 0 ? (y) - 1 : -((y) - 1))) / (y))



/**

 * Standard input helpers

 */

// vector:input

template <typename T>

istream &operator>>(istream &is, vector<T> &vec) {

    for (T &x : vec) is >> x;

    return is;

}

// pair:output

template <typename T, typename U>

ostream &operator<<(ostream &os, pair<T, U> &pair_var) {

    os << "(" << pair_var.first << ", " << pair_var.second << ")";

    return os;

}

// vector:output

template <typename T>

ostream &operator<<(ostream &os, const vector<T> &vec) {

    os << "{";

    for (int i = 0; i < vec.size(); i++) {

        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");

    }

    os << "}";

    return os;

}

// map:output

template <typename T, typename U>

ostream &operator<<(ostream &os, map<T, U> &map_var) {

    os << "{";

    repi(itr, map_var) {

        os << *itr;

        itr++;

        if (itr != map_var.end()) os << ", ";

        itr--;

    }

    os << "}";

    return os;

}

// set:output

template <typename T>

ostream &operator<<(ostream &os, set<T> &set_var) {

    os << "{";

    repi(itr, set_var) {

        os << *itr;

        itr++;

        if (itr != set_var.end()) os << ", ";

        itr--;

    }

    os << "}";

    return os;

}

/**

 * Debug tools

 */

#define DUMPOUT cout

void dump_func() {

    DUMPOUT << endl;

}

template <class Head, class... Tail>

void dump_func(Head &&head, Tail &&... tail) {

    DUMPOUT << head;

    if (sizeof...(Tail) > 0) {

        DUMPOUT << ", ";

    }

    dump_func(std::move(tail)...);

}

#ifdef LOCAL_

#define dump(...)                                                              \

    DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \

            << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \

            << endl                                                            \

            << "    ",                                                         \

        dump_func(__VA_ARGS__)

#else

#define dump(...)

#endif



/**

 * Helpers

 */

using lli = long long int;

using ii = pair<int, int>;

template<typename T>

using heapq = priority_queue<T, vector<T>, greater<T> >; // implement <

int dx[4]={1,0,-1,0};

int dy[4]={0,1,0,-1};

int exdx[8]={1, 1, 1, 0, 0, -1, -1, -1};

int exdy[8]={1, 0, -1, 1, -1, 1, 0, -1};

struct aaa{

    aaa(){

        cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);

    };

}aaaaaaa;

/**

 * Libraries

 */

vector<int> sieve(int n) {

    vector<int> ret;

    vector<int> is_prime_arr(n, 1);

    for (int i = 2; i < n; i++) {

        if (is_prime_arr[i]) {

            ret.push_back(i);

            for (int j = 2 * i; j < n; j += i) is_prime_arr[j] = 0;

        }

    }

    return ret;

}

/**

 * If necessary

 */

// #define int long long int

// #define INF (1e9 + 1)

/**

 * Solver

 */

signed main() {

    const int NUM_MAX = (int)1e5 + 1;

    vector<int> ary(NUM_MAX, 0);

    vector<int> primes = sieve(NUM_MAX);

    for (int p: primes) {

        if (p >= 3) ++ary[p];

        if (2 * p - 1 < NUM_MAX) {

            ++ary[2 * p - 1];

        }

    }

    rep(i, NUM_MAX) {

        ary[i] = ary[i] == 2 ? 1 : 0;

    }

    repx(i, 1, NUM_MAX, 1) {

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

    }



    int Q; cin >> Q;

    rept(Q) {

        int L, R; cin >> L >> R;

        cout << ary[R] - ary[L - 1] << endl;

    }

    return 0;

}


// Fastest known solution for this problem: