Problem:
Score : 400 points

Problem Statement

We say that a odd number N is similar to 2017 when both N and (N+1)/2 are prime.

You are given Q queries.

In the i-th query, given two odd numbers l_i and r_i, find the number of odd numbers x similar to 2017 such that l_i ≤ x ≤ r_i.

Constraints

1≤Q≤10^5

1≤l_i≤r_i≤10^5

l_i and r_i are odd.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line (1≤i≤Q) should contain the response to the i-th query.

Sample Input 1

1
3 7

Sample Output 1

2

3 is similar to 2017, since both 3 and (3+1)/2=2 are prime.

5 is similar to 2017, since both 5 and (5+1)/2=3 are prime.

7 is not similar to 2017, since (7+1)/2=4 is not prime, although 7 is prime.

Thus, the response to the first query should be 2.

Sample Input 2

4
13 13
7 11
7 11
2017 2017

Sample Output 2

1
0
0
1

Note that 2017 is also similar to 2017.

Sample Input 3

6
1 53
13 91
37 55
19 51
73 91
13 49

Sample Output 3

4
4
1
1
1
2

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0;i < (int)(n);i++)
using ll = long long;
const ll MOD=1000000007;
//const ll MOD=998244353;
const long long INF = 1LL << 60;
const double pi=acos(-1.0);
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

/*複数の整数に対する素因数分解、素数判定*/

//エラトステネスの篩
//前処理でmake_eratos() デフォルトで150msくらい
const int MAX_for_eratos=10000100;
int eratos[MAX_for_eratos];
int prime_number_list[MAX_for_eratos/2];

void make_eratos(){
  rep(i,MAX_for_eratos) eratos[i]=-1;
  //2の倍数だけ先にやっちゃう
  prime_number_list[0]=2;
  for(int j=2;j<MAX_for_eratos;j+=2) eratos[j]=2; 
  int k=1;
  for(int i=3;i<MAX_for_eratos;i+=2){
    if(eratos[i]>-1) continue;
    prime_number_list[k]=i; k++;
    eratos[i]=i;
    if(i>MAX_for_eratos/i+1) continue;
    for(int j=i*i;j<MAX_for_eratos;j+=i) if(eratos[j]==-1) eratos[j]=i;
  }
  
  return;
}
//osa_k法による素因数分解
//普通の因数分解と同様N==1の時に注意
//前処理make_eratos必要
vector<pair<ll,ll>> prime_factorize(ll N){
  vector<pair<ll,ll>> ret={};
  for(int i=0;N>MAX_for_eratos-100&&prime_number_list[i]*(ll)prime_number_list[i]<=N;i++){
    if(N%prime_number_list[i]!=0) continue;
    ll a=prime_number_list[i];
    ll ex=0;
    while(N%a==0){
      ex++;
      N/=a;
    }
    ret.push_back({a,ex});
  }
  if(N>MAX_for_eratos-100) {ret.push_back({N,1}); return ret;}
  while(N>1){
    ll ex=0;
    ll a=eratos[N];
    while(N%a==0){
      ex++;
      N/=a;
    }
    ret.push_back({a,ex});
  }
  
  return ret;
}

//素数判定
//前処理make_eratos必要
bool is_prime(ll N) {
  if(N==1) return false;
  if(N<=MAX_for_eratos-100) return N==eratos[N];
  else{
    for(int i=0;prime_number_list[i]*(ll)prime_number_list[i]<=N;i++){
      if(N%prime_number_list[i]==0) return false;
    }
    return true;
  }
}

/*終わり*/

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout << fixed << setprecision(15);

  make_eratos();
  vector<ll> vec(100001,0); rep(i,100000) if(i+1!=2&&is_prime(i+1)&&is_prime((i+2)/2)) vec[i+1]=1;
  vector<ll> cum(100002,0); rep(i,100001) cum[i+1]=cum[i]+vec[i];
  
  /*
  rep(i,8){
    if(i!=0) cout<<" ";
    cout<<vec[i];
  }
  cout<<endl;
  */
  
  ll Q; cin>>Q;
  rep(i,Q){
    ll l,r; cin>>l>>r;
    cout<<cum[r+1]-cum[l]<<'\n';
  }
  return 0;
}