stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



using VS = vector<string>;    using LL = long long;

using VI = vector<int>;       using VVI = vector<VI>;

using PII = pair<int, int>;   using PLL = pair<LL, LL>;

using VL = vector<LL>;        using VVL = vector<VL>;



#define ALL(a)  begin((a)),end((a))

#define RALL(a) (a).rbegin(), (a).rend()

#define PB push_back

#define EB emplace_back

#define MP make_pair

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

#define SORT(c) sort(ALL((c)))

#define RSORT(c) sort(RALL((c)))

#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)

#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)

#define debug(x) cerr << #x << ": " << x << endl

const int INF = 1e9;                          const LL LINF = 1e16;

const LL MOD = 1000000007;                    const double PI = acos(-1.0);

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



/* -----  2018/02/23  Problem: 084_abc_d / Link: https://abc084.contest.atcoder.jp/tasks/abc084_d?lang=en  ----- */

/* ------問題------



「N も (N+1)÷2 も素数」を満たす奇数 N を 2017に似た数 とします。

Q 個のクエリが与えられます。

クエリ i(1≦i≦Q) では奇数 li,ri が与えられるので、li≦x≦ri かつ 2017に似た数 となる奇数 x の個数を求めてください。



-----問題ここまで----- */

/* -----解説等-----



素数表ができれば後は適当に2017っぽい数がわかるので累積和をとれば十分高速。



----解説ここまで---- */



LL N;



LL ans = 0LL;

#define primeN 100000

bool prime[primeN + 1];// 外部だとハッシュ表みたいになる

void make_prime() {

	FOR(i, 0, primeN + 1) {

		prime[i] = 1;

	}prime[0] = prime[1] = 0;



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

		if (prime[i]) {

			for (int j = i * 2; j <= primeN; j += i)

				prime[j] = 0;

		}

	}

}

#undef primeN 



int main() {

	cin.tie(0);

	ios_base::sync_with_stdio(false);



	make_prime();



	VI like2017(100001, 0);

	FOR(i, 1, 100001) {

		if (i % 2 == 1 && prime[i] && prime[(i + 1) / 2]) {

			like2017[i] = 1;

		}

	}

	VI sum(100002, 0);

	FOR(i, 0, 100001) {

		sum[i + 1] += sum[i]+ like2017[i];

	}



	cin >> N;

	FOR(i, 0, N) {

		int L, R;

		cin >> L >> R;

		cout << sum[R+1] - sum[L] << endl;

	}



	return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

const int n=1e5+10;

bitset<n> isprime;

vector<int>answer;

void sieve()

{

    isprime[0]=isprime[1]=0;

    for(int p=2; p<=n; ++p)

        if(isprime[p])

            for(int m=2*p; m<n; m+=p)

                isprime[m]=0;



}





int the2017ers[n];

void is2017()

{

    int sum=0;

    for (int p=1; p<=n; p++)

    {

        sum+=(isprime[p]&&isprime[(p+1)/2]);

        the2017ers[p]=sum;



    }



}



int main()

{

     ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    isprime.set();

    sieve();

    is2017();

    int tc,l,r;

    cin>>tc;

    while(tc--)

    {

        cin>>l>>r;

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

    }





    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):


#include <iostream>

#include <cstdio>

#include<string>

#include<cstring>

#include <algorithm>

#include <cmath>

#include <map>



#define N 100005



using namespace std;



int vis[N],a[N];

void sushu() {

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

		if(!vis[i]) {

			for(int j=i*2; j<=N; j+=i)

				vis[j]=1;

		}

	}

}



int main() {

	memset(vis,0,sizeof(vis));

	sushu();

	vis[1]=1;

	int sum=0,n;

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

		if(i%2==1) {

			if(vis[i]==0&&vis[(i+1)/2]==0)

				sum++;

		}

		a[i]=sum;

	}

	scanf("%d",&n);



	while(n--) {

		int c,b;

		scanf("%d%d",&c,&b);

		printf("%d\n",a[b]-a[c-1]);

	}



}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

const int n=1e5+10;

bitset<n> isprime;

vector<int>answer;

void sieve()

{

    isprime[0]=isprime[1]=0;

    for(int p=2; p<=n; ++p)

        if(isprime[p])

            for(int m=2*p; m<n; m+=p)

                isprime[m]=0;



}





int the2017ers[n];

void is2017()

{

    int sum=0;

    for (int p=1; p<=n; p++)

    {

        sum+=(isprime[p]&&isprime[(p+1)/2]);

        the2017ers[p]=sum;



    }



}



int main()

{

     ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    isprime.set();

    sieve();

    is2017();

    int tc,l,r;

    cin>>tc;

    while(tc--)

    {

        cin>>l>>r;

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

    }





    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <bits/stdc++.h>

using namespace std;



#define pb push_back

#define F first

#define S second

#define fo(i,a,b)  for(int i = a; i < b; i++)



using ll = long long;

using ld = long double;

using ul = unsigned long long;

using ii = pair<int, int>;

#define Mosta     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define ReadFile            freopen("input.in","r",stdin)

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

const ll mod7 = 1000000007,mod = 1000000000,OO = 0x3f3f3f3f;

const ld EPS = 1e-7;

double Pi=acos(-1);

const ld rad=Pi/180.0;

ll power(ll x, ll y, ll p)

{

    ll res = 1;

    while (y > 0)

    {

        if (y & 1)

            res = (res*x) % p;

        y = y>>1;

        x = (x*x) % p;

    }

    return res;

}

ll nCr (ll n,ll m)

{

        return(m == 0)?1:n*nCr(n-1,m-1)/m;

}

/*****************************************************/



const int N = (int) 1e5 + 5;



bool prime[N];



int ans[N];



int main()

{

    memset(prime , 1 , sizeof(prime));

    prime[0]=prime[1]=0;

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

        if(prime[i]){

            for(int j = i+i; j <= N; j+=i)

                prime[j] = 0;

        }

    }



    for(int i = 3; i <= 100000; i+=2){

        if(prime[i] && prime[(i+1)/2])

            ans[i]++;

    }



    for(int i = 3; i <= 100000; i++)

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



    int q;

    cin >> q;



    while(q--){

        int l , r;

        cin >> l >> r;

        cout << ans[r] - ans[l-1] << "\n";

    }



    return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

const int n=1e5+10;

bitset<n> isprime;

vector<int>answer;

void sieve()

{

    isprime[0]=isprime[1]=0;

    for(int p=2; p<=n; ++p)

        if(isprime[p])

            for(int m=2*p; m<n; m+=p)

                isprime[m]=0;



}





int the2017ers[n];

void is2017()

{

    int sum=0;

    for (int p=1; p<=n; p++)

    {

        sum+=(isprime[p]&&isprime[(p+1)/2]);

        the2017ers[p]=sum;



    }



}



int main()

{

     ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    isprime.set();

    sieve();

    is2017();

    int tc,l,r;

    cin>>tc;

    while(tc--)

    {

        cin>>l>>r;

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

    }





    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

#define N 100005

using namespace std;

int n, l, r, pr[N], s[N];

int main() {

	int i, j;

	for(i=2; i<N; i++) if(!pr[i])

		for(j=i*2; j<N; j+=i) pr[j]=1;

	for(i=3; i<N; i+=2) {

		s[i] = s[i-2];

		if(!pr[i] && !pr[(i+1)/2]) s[i]++;

	}

	scanf("%d", &n);

	while(n--) {

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

		printf("%d\n", s[r] - s[max(l-2, 1)]);

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#define N 100005

int n, l, r, pr[N], s[N];

int main() {

	int i, j;

	for(i=2; i*i<N; i++) if(!pr[i])

		for(j=i*i; j<N; j+=i) pr[j]=1;

	for(i=3; i<N; i++) s[i] = s[i-1] + (!pr[i] & !pr[(i+1)/2]);

	scanf("%d", &n);

	while(n--) {

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

		printf("%d\n", s[r] - s[l-1]);

	}

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <algorithm>

#include <vector>

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

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

using namespace std;

// エラストテネスの篩

const int NMAX = 1e+5+1;

int prime[NMAX];

void Sieve(){

  rep(i,NMAX) prime[i] = true;

  prime[0] = prime[1] = false;

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

    if(prime[i]){

      int j = i*2;

      while(j<=NMAX){

	prime[j] = false;

	j += i;

      }

    }

  }

}

int main()

{

  int q;cin >> q;

  vector<int> l(q),r(q);

  rep(i,q) cin >> l[i] >> r[i];

  Sieve();

  vector<int> sum(1e+5+2);

  sum[0] = 0;

  rep(i,NMAX){

    if(i%2==1){

      if(prime[i]&&prime[(i+1)/2]){

	sum[i+1] = sum[i] + 1;

	continue;

      }

    }

    sum[i+1] = sum[i];

  }

  rep(i,q) cout << sum[r[i]+1]-sum[l[i]] << "\n";

  

  return 0;

}


// Optimized solution to the same problem (faster):
#include <iostream>

#include <algorithm>

#include <vector>

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

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

using namespace std;

template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; }

template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; }



const int NMAX = 1e+5+1;

bool prime[NMAX];

void Sieve(){

  rep(i,NMAX) prime[i] = true;

  prime[0] = prime[1] = false;

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

    if(prime[i]){

      int j = i*2;

      while(j<=NMAX){

	prime[j] = false;

	j += i;

      }

    }

  }

}



int main()

{

  int q;cin >> q;

  vector<int> l(q), r(q);

  rep(i,q) cin >> l[i] >> r[i];



  Sieve();

  vector<int> sum(NMAX+1);

  sum[0] = 0;

  rep(i,NMAX) {

    int x = 0;

    if(prime[i] && prime[(i+1)/2]) {

      x = 1;

    }

    sum[i+1] = sum[i] + x;

  }



  rep(i,q) {

    cout << sum[r[i]+1] - sum[l[i]] << "\n";

  }

	    

  

  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



#define N 100000



int main(void){

    bool prime[N+1];

    for (size_t i = 2; i <= N; i++)

    {

        prime[i]=true;

    }

    const int max = sqrt(N);

    for (size_t i = 0; i <= max; i++)

    {

        if (prime[i])

        {

            for (size_t j = 2; i*j <= N; j++)

            {

                prime[i*j]=false;

            }

        }

    }

    int likeCnt[N]={};

    for (size_t i = 1; i <= N; i++)

    {

        likeCnt[i]=likeCnt[i-1];

        if (i%2==1&&prime[i]&&prime[(i+1)/2])

        {

            likeCnt[i]++;

        }

    }

    /**************************************/

    int q;

    cin >> q;

    int l[q],r[q];

    for (size_t i = 0; i < q; i++)

    {

        cin >>l[i]>>r[i];

    }

    for (size_t i = 0; i < q; i++)

    {

        cout << likeCnt[r[i]]-likeCnt[l[i]-1] << endl;

    }

    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



#define N 100000



int main(void){

    vector<bool> prime(N+1,true);

    prime[0]=prime[1]=false;

    for (size_t i = 2; i <= sqrt(N); i++)

    {

        if (prime[i])

        {

            for (size_t j = 2; i*j <= N; j++)

            {

                prime[i*j]=false;

            }

        }

    }

    int likeCnt[N+1];

    for (size_t i = 3; i <= N; i+=2)

    {

        likeCnt[i]=likeCnt[i-1];

        if (i%2==1&&prime[i]&&prime[(i+1)/2]) likeCnt[i]++;

        likeCnt[i+1]=likeCnt[i];

    }

    /**************************************/

    int q;

    scanf("%d", &q);

    int l,r;

    for (size_t i = 0; i < q; i++)

    {

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

        printf("%d\n", likeCnt[r] - likeCnt[l-1]);

    }

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



#define N 100000



int main(void){

    bool prime[N+1];

    for (size_t i = 0; i <= N; i++)

    {

        prime[i]=true;

    }

    for (size_t i = 0; i < 2; i++)

    {

        prime[i]=false;

    }

    for (size_t i = 0; i <= sqrt(N); i++)

    {

        if (!prime[i])continue;

        else

        {

            for (size_t j = 2; i*j <= N; j++)

            {

                prime[i*j]=false;

            }

        }

    }

    int likePrime[N]={};

    int likeCnt[N]={};

    for (size_t j = 1; j <= N; j++)

    {

        likeCnt[j]=likeCnt[j-1];

        if (j%2==1&&prime[j]&&prime[(j+1)/2])

        {

            likePrime[j]=true;

            likeCnt[j]++;

        }

    }

    /**************************************/

    int q;

    cin >> q;

    int l[q],r[q];

    for (size_t i = 0; i < q; i++)

    {

        cin >>l[i]>>r[i];

    }

    int ans[q]={};

    for (size_t i = 0; i < q; i++)

    {

        cout << likeCnt[r[i]]-likeCnt[l[i]-1] << endl;

    }

    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



#define N 100000



int main(void){

    vector<bool> prime(N+1,true);

    prime[0]=prime[1]=false;

    for (size_t i = 2; i <= sqrt(N); i++)

    {

        if (prime[i])

        {

            for (size_t j = 2; i*j <= N; j++)

            {

                prime[i*j]=false;

            }

        }

    }

    int likeCnt[N+1];

    for (size_t i = 3; i <= N; i+=2)

    {

        likeCnt[i]=likeCnt[i-1];

        if (i%2==1&&prime[i]&&prime[(i+1)/2]) likeCnt[i]++;

        likeCnt[i+1]=likeCnt[i];

    }

    /**************************************/

    int q;

    scanf("%d", &q);

    int l,r;

    for (size_t i = 0; i < q; i++)

    {

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

        printf("%d\n", likeCnt[r] - likeCnt[l-1]);

    }

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



#define N 100000



int main(void){

    bool prime[N+1];

    for (size_t i = 2; i <= N; i++)

    {

        prime[i]=true;

    }

    for (size_t i = 0; i <= sqrt(N); i++)

    {

        if (prime[i])

        {

            for (size_t j = 2; i*j <= N; j++)

            {

                prime[i*j]=false;

            }

        }

    }

    int likeCnt[N]={};

    for (size_t j = 1; j <= N; j++)

    {

        likeCnt[j]=likeCnt[j-1];

        if (j%2==1&&prime[j]&&prime[(j+1)/2])

        {

            likeCnt[j]++;

        }

    }

    /**************************************/

    int q;

    cin >> q;

    int l[q],r[q];

    for (size_t i = 0; i < q; i++)

    {

        cin >>l[i]>>r[i];

    }

    int ans[q]={};

    for (size_t i = 0; i < q; i++)

    {

        cout << likeCnt[r[i]]-likeCnt[l[i]-1] << endl;

    }

    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



#define N 100000



int main(void){

    vector<bool> prime(N+1,true);

    prime[0]=prime[1]=false;

    for (size_t i = 2; i <= sqrt(N); i++)

    {

        if (prime[i])

        {

            for (size_t j = 2; i*j <= N; j++)

            {

                prime[i*j]=false;

            }

        }

    }

    int likeCnt[N+1];

    for (size_t i = 3; i <= N; i+=2)

    {

        likeCnt[i]=likeCnt[i-1];

        if (i%2==1&&prime[i]&&prime[(i+1)/2]) likeCnt[i]++;

        likeCnt[i+1]=likeCnt[i];

    }

    /**************************************/

    int q;

    scanf("%d", &q);

    int l,r;

    for (size_t i = 0; i < q; i++)

    {

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

        printf("%d\n", likeCnt[r] - likeCnt[l-1]);

    }

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



#define N 100000



int main(void){

    vector<bool> prime(N+1,true);

    prime[0]=prime[1]=false;

    for (size_t i = 0; i <= sqrt(N); i++)

    {

        if (prime[i])

        {

            for (size_t j = 2; i*j <= N; j++)

            {

                prime[i*j]=false;

            }

        }

    }

    vector<int> likeCnt(N);

    for (size_t i = 1; i <= N; i++)

    {

        likeCnt[i]=likeCnt[i-1];

        if (i%2==1&&prime[i]&&prime[(i+1)/2])

        {

            likeCnt[i]++;

        }

    }

    /**************************************/

    int q;

    cin >> q;

    vector<int> l(q),r(q);

    for (size_t i = 0; i < q; i++)

    {

        cin >>l[i]>>r[i];

    }

    for (size_t i = 0; i < q; i++)

    {

        cout << likeCnt[r[i]]-likeCnt[l[i]-1] << endl;

    }

    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



#define N 100000



int main(void){

    vector<bool> prime(N+1,true);

    prime[0]=prime[1]=false;

    for (size_t i = 2; i <= sqrt(N); i++)

    {

        if (prime[i])

        {

            for (size_t j = 2; i*j <= N; j++)

            {

                prime[i*j]=false;

            }

        }

    }

    int likeCnt[N+1];

    for (size_t i = 3; i <= N; i+=2)

    {

        likeCnt[i]=likeCnt[i-1];

        if (i%2==1&&prime[i]&&prime[(i+1)/2]) likeCnt[i]++;

        likeCnt[i+1]=likeCnt[i];

    }

    /**************************************/

    int q;

    scanf("%d", &q);

    int l,r;

    for (size_t i = 0; i < q; i++)

    {

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

        printf("%d\n", likeCnt[r] - likeCnt[l-1]);

    }

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



#define N 100000



int main(void){

    vector<bool> prime(N+1,true);

    prime[0]=prime[1]=false;

    for (size_t i = 0; i <= sqrt(N); i++)

    {

        if (prime[i])

        {

            for (size_t j = 2; i*j <= N; j++)

            {

                prime[i*j]=false;

            }

        }

    }

    vector<int> likeCnt(N);

    for (size_t i = 1; i <= N; i++)

    {

        likeCnt[i]=likeCnt[i-1];

        if (i%2==1&&prime[i]&&prime[(i+1)/2])

        {

            likeCnt[i]++;

        }

    }

    /**************************************/

    int q;

    cin >> q;

    int l[q],r[q];

    for (size_t i = 0; i < q; i++)

    {

        cin >>l[i]>>r[i];

    }

    for (size_t i = 0; i < q; i++)

    {

        cout << likeCnt[r[i]]-likeCnt[l[i]-1] << endl;

    }

    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



#define N 100000



int main(void){

    vector<bool> prime(N+1,true);

    prime[0]=prime[1]=false;

    for (size_t i = 2; i <= sqrt(N); i++)

    {

        if (prime[i])

        {

            for (size_t j = 2; i*j <= N; j++)

            {

                prime[i*j]=false;

            }

        }

    }

    int likeCnt[N+1];

    for (size_t i = 3; i <= N; i+=2)

    {

        likeCnt[i]=likeCnt[i-1];

        if (i%2==1&&prime[i]&&prime[(i+1)/2]) likeCnt[i]++;

        likeCnt[i+1]=likeCnt[i];

    }

    /**************************************/

    int q;

    scanf("%d", &q);

    int l,r;

    for (size_t i = 0; i < q; i++)

    {

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

        printf("%d\n", likeCnt[r] - likeCnt[l-1]);

    }

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



#define N 100000



int main(void){

    vector<bool> prime(N+1,true);

    prime[0]=prime[1]=false;

    for (size_t i = 0; i <= sqrt(N); i++)

    {

        if (prime[i])

        {

            for (size_t j = 2; i*j <= N; j++)

            {

                prime[i*j]=false;

            }

        }

    }

    vector<int> likeCnt(N);

    for (size_t i = 1; i <= N; i++)

    {

        likeCnt[i]=likeCnt[i-1];

        if (i%2==1&&prime[i]&&prime[(i+1)/2])

        {

            likeCnt[i]++;

        }

    }

    /**************************************/

    int q;

    cin >> q;

    int l[q],r[q];

    for (size_t i = 0; i < q; i++)

    {

        cin >> l[i] >> r[i];

        cout << likeCnt[r[i]] - likeCnt[l[i]-1] << endl;

    }

    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



#define N 100000



int main(void){

    vector<bool> prime(N+1,true);

    prime[0]=prime[1]=false;

    for (size_t i = 2; i <= sqrt(N); i++)

    {

        if (prime[i])

        {

            for (size_t j = 2; i*j <= N; j++)

            {

                prime[i*j]=false;

            }

        }

    }

    int likeCnt[N+1];

    for (size_t i = 3; i <= N; i+=2)

    {

        likeCnt[i]=likeCnt[i-1];

        if (i%2==1&&prime[i]&&prime[(i+1)/2]) likeCnt[i]++;

        likeCnt[i+1]=likeCnt[i];

    }

    /**************************************/

    int q;

    scanf("%d", &q);

    int l,r;

    for (size_t i = 0; i < q; i++)

    {

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

        printf("%d\n", likeCnt[r] - likeCnt[l-1]);

    }

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



#define N 100000



int main(void){

    ios::sync_with_stdio(false);

    vector<bool> prime(N+1,true);

    prime[0]=prime[1]=false;

    for (size_t i = 0; i <= sqrt(N); i++)

    {

        if (prime[i])

        {

            for (size_t j = 2; i*j <= N; j++)

            {

                prime[i*j]=false;

            }

        }

    }

    vector<int> likeCnt(N);

    for (size_t i = 1; i <= N; i++)

    {

        likeCnt[i]=likeCnt[i-1];

        if (i%2==1&&prime[i]&&prime[(i+1)/2])

        {

            likeCnt[i]++;

        }

    }

    /**************************************/

    int q;

    cin >> q;

    int l[q],r[q];

    for (size_t i = 0; i < q; i++)

    {

        cin >> l[i] >> r[i];

        cout << likeCnt[r[i]] - likeCnt[l[i]-1] << "\n";

    }

    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



#define N 100000



int main(void){

    vector<bool> prime(N+1,true);

    prime[0]=prime[1]=false;

    for (size_t i = 2; i <= sqrt(N); i++)

    {

        if (prime[i])

        {

            for (size_t j = 2; i*j <= N; j++)

            {

                prime[i*j]=false;

            }

        }

    }

    int likeCnt[N+1];

    for (size_t i = 3; i <= N; i+=2)

    {

        likeCnt[i]=likeCnt[i-1];

        if (i%2==1&&prime[i]&&prime[(i+1)/2]) likeCnt[i]++;

        likeCnt[i+1]=likeCnt[i];

    }

    /**************************************/

    int q;

    scanf("%d", &q);

    int l,r;

    for (size_t i = 0; i < q; i++)

    {

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

        printf("%d\n", likeCnt[r] - likeCnt[l-1]);

    }

    return 0;

}

// Fastest known solution for this problem: