stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std ;



const int MAX = 1e5 + 10 ;



int prime[MAX] , ans[MAX];



int main()

{

	ios_base::sync_with_stdio(0) ;

	cin.tie(0) ;

	for(int i = 0 ; i < MAX ; ++i)

		prime[i] = 1 ; 

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

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

	{

		if(prime[i])

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

				prime[j] = 0 ;

	}

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

	{

		ans[i] = ans[i-1] + (bool)(prime[i] && prime[(i+1)/2]);

	}

	int q ;

	cin>>q ;

	while(q--)

	{

		int l , r ;

		cin>>l>>r ;

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

	}

	return 0 ;

}

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

using namespace std;



#define df(b, e) ((b) > (e))

#define fore(i, b, e) for(auto i = (b) - df(b, e); i != e - df(b, e); i += 1 - 2 * df(b, e))

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

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

#define f first

#define s second

#define pb push_back



typedef long long lli;

typedef long double ld;

typedef pair<int, int> ii;



template <class T>

struct rge { T b, e; };



template <class T>

rge<T> range(T i, T j) {

  return rge<T>{i, j};

}



template <class T> auto dud(T *x) -> decltype(cerr << *x, 0);

template <class T> char dud(...);



struct debug {

#ifdef LOCAL

  ~debug() { cout << endl; }



  template <class T>

  typename enable_if<sizeof dud<T>(0) != 1, debug &>::type operator << (T i) {

    cout << boolalpha << i;

    return *this;

  }



  template <class T>

  typename enable_if<sizeof dud<T>(0) == 1, debug &>::type operator << (T i) {

    return *this << range(begin(i), end(i));

  }



  template <class A, class B>

  debug & operator << (pair<A, B> d) {

    return *this << "(" << d.first << ", " << d.second << ")";

  }



  template <class T>

  debug & operator << (rge<T> d) {

    *this << "[";

    for (auto it = d.b; it != d.e; ++it)

      *this << ", " + 2 * (it == d.b) << *it;

    return *this << "]";

  }

#else

  template <class T> 

  debug & operator << (const T&) {

    return *this; 

  }

#endif

};



#define deb(...) " " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " "



const int N = 1e5 + 5;



bitset<N> isp;

int sim[N];

int q, l, r;



int main() {

  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  isp.set();

  isp[0] = isp[1] = 0;

  fore (i, 2, N) {

    if (isp[i]) {

      sim[i] += isp[(i + 1) / 2];

      if (1LL * i * i < N)

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

          isp[j] = 0;

    }

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

  }

  cin >> q;

  while (cin >> l >> r) 

    cout << sim[r] - sim[l - 1] << '\n';

  return 0;

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

using namespace std;

int const N=1e5;

int prime[N];

int ans[N];

int main() {

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

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

        prime[i]=1;

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

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

{

    if(prime[i]==1)

    {

        //cout<<i;

        for(long long  j=i*i;j<=N;j+=i)

        {



            //cout<<j;

         prime[j]=0;

        }

    }

}



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

{

   int  x=(i+1)/2;

    if(prime[i]&&prime[x])

    {

       // prime[i]=1;

       ans[i]=1;

   // cout<<"yes";

    }

    else

        ans[i]=0;

   // cout<<i;

}



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

{

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

}



int q;

cin>>q;

while(q--)

{

    int l,r;

    cin>>l>>r;



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

}

}


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

using namespace std;



#define df(b, e) ((b) > (e))

#define fore(i, b, e) for(auto i = (b) - df(b, e); i != e - df(b, e); i += 1 - 2 * df(b, e))

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

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

#define f first

#define s second

#define pb push_back



typedef long long lli;

typedef long double ld;

typedef pair<int, int> ii;



template <class T>

struct rge { T b, e; };



template <class T>

rge<T> range(T i, T j) {

  return rge<T>{i, j};

}



template <class T> auto dud(T *x) -> decltype(cerr << *x, 0);

template <class T> char dud(...);



struct debug {

#ifdef LOCAL

  ~debug() { cout << endl; }



  template <class T>

  typename enable_if<sizeof dud<T>(0) != 1, debug &>::type operator << (T i) {

    cout << boolalpha << i;

    return *this;

  }



  template <class T>

  typename enable_if<sizeof dud<T>(0) == 1, debug &>::type operator << (T i) {

    return *this << range(begin(i), end(i));

  }



  template <class A, class B>

  debug & operator << (pair<A, B> d) {

    return *this << "(" << d.first << ", " << d.second << ")";

  }



  template <class T>

  debug & operator << (rge<T> d) {

    *this << "[";

    for (auto it = d.b; it != d.e; ++it)

      *this << ", " + 2 * (it == d.b) << *it;

    return *this << "]";

  }

#else

  template <class T> 

  debug & operator << (const T&) {

    return *this; 

  }

#endif

};



#define deb(...) " " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " "



const int N = 1e5 + 5;



bitset<N> isp;

int sim[N];

int q, l, r;



int main() {

  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  isp.set();

  isp[0] = isp[1] = 0;

  fore (i, 2, N) {

    if (isp[i]) {

      sim[i] += isp[(i + 1) / 2];

      if (1LL * i * i < N)

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

          isp[j] = 0;

    }

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

  }

  cin >> q;

  while (cin >> l >> r) 

    cout << sim[r] - sim[l - 1] << '\n';

  return 0;

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

using namespace std;

int a,b,n;

vector<int> v;

bool prime[501010];

void SieveOfEratosthenes()

{

    int n=401010;



    memset(prime, true, sizeof(prime));



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

    {

        if (prime[p] == true)

        {

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

                prime[i] = false;

        }

    }

    for (int p=3; p<=101010; p++)

    {

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

        {

            v.push_back(p);

        }

    }

}

void init()

{

    SieveOfEratosthenes();

    /*cout<<v.size()<<endl;

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

    {

        cout<<v[i]<<endl;

    }*/

}

int bcount(int l,int r)

{

    std::vector<int>::iterator low,up;

    low=std::lower_bound (v.begin(), v.end(), l); //          ^

    up= std::upper_bound (v.begin(), v.end(), r); //                   ^

    return (up-v.begin())-(low-v.begin());

}

int main()

{

    init();

    int q,l,r;

    cin>>q;

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

    {

        cin>>l>>r;

        cout<<bcount(l,r)<<endl;

    }

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.443992214705207x):
#include <iostream> 

using namespace std;



int sushu[100001]={0};//素数表 

int qianzhui[100001]={0};//前缀和 

void sieve(){

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

		if(sushu[i]==0){//合数的倍数不需要再做筛选 

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

			    //要把合数下标置1

			    sushu[i*j]=1;

		    }

		}

	}

}

int main(){

	int q,l,r;

	cin>>q;

	sieve();

	sushu[1]=1;

	sushu[2]=1;

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

		if(sushu[i]==0&&sushu[(i+1)/2]==0&&i!=2||i==3){

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

		}else{

			qianzhui[i]=qianzhui[i-1];

		}

	}

	for(int j=1;j<=q;j++){

		cin>>l>>r;

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

	} 

    return 0;

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

using namespace std;







int isPrime[100019], okay[100019], cum[100019];

void sieve()

{



    for(int i=2;i<=100009;i+=2)

        isPrime[i]=1;



     isPrime[2]=0;



    for(int i=3; i<=100009; i+=2)

    {

        if(isPrime[i]==0)

        {

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

                okay[i]=1;



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

            {

                isPrime[i*j]=1;

            }



        }

    }



    isPrime[1]=1;





//    for(int i=1; i<=10; i++)

//    {

//        if(okay[i]==1)

//        {

//            printf("%d\n",i);

//        }

//    }









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

    {

        cum[i]= cum[i-1]+okay[i];

       // printf("%d\n",cum[i]);

    }









}



int main()

{



    sieve();



    int q,l,r;

    scanf("%d",&q);





    while(q--)

    {

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



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







    }





}






// Faster C++ solution for the same problem (measured speedup: 1.1718113455720638x):
#include <iostream> 

using namespace std;



int sushu[100001]={0};//素数表 

int qianzhui[100001]={0};//前缀和 

void sieve(){

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

		if(sushu[i]==0){//合数的倍数不需要再做筛选 

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

			    //要把合数下标置1

			    sushu[i*j]=1;

		    }

		}

	}

}

int main(){

	int q,l,r;

	cin>>q;

	sieve();

	sushu[1]=1;

	sushu[2]=1;

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

		if(sushu[i]==0&&sushu[(i+1)/2]==0&&i!=2||i==3){

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

		}else{

			qianzhui[i]=qianzhui[i-1];

		}

	}

	for(int j=1;j<=q;j++){

		cin>>l>>r;

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

	} 

    return 0;

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

using namespace std;



int dp[100009], total[100009];

int store[100009], mark[100009], nPrime, isPrime[100009];



///seieve

void seive(int n)

{

    int i,j, lim = sqrt(n*1.0) +2;



    mark[1] = 1;

    for(i = 4; i<=n; i+=2) mark[i] = 1;



    store[nPrime++] = 2;



    isPrime[2] = 1;



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

    {

        if(!mark[i])

        {

            store[nPrime++] = i;

            isPrime[i] = 1;



            if(i<=lim)

            {

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

                {

                    mark[j]=1;

                }

            }

        }

    }





}



int f(int n) /// is like 2017

{

    if(dp[n]!=-1) return dp[n];

    if(n%2==0) return dp[n] = 0;

    if(isPrime[n]==1 && isPrime[ (n+1)/2 ]==1) return dp[n] = 1;

    else return dp[n] = 0;

}



void cnt(int N)

{

    for(int i=0; i<=N; i++) f(i);

    total[1] = 0;

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

    {

        total[i] = total[i-1]+dp[i];

    }

}



int main()

{

    int q;

    int l, r;

    int ans = 0;



    seive(100002);

    memset(dp,-1,sizeof(dp));

    cnt(100002);



    //for(int i=0; i<nPrime; i++) cout<<store[i]<<" ";

    //for(int i=0; i<100000; i++) if(dp[i]) cout<<i<<" "<<total[i]<<" "<<endl;



    cin>>q;

    while(q--)

    {

        cin>>l>>r;

        if(l==1) ans = total[r];

        else ans = total[r] - total[l-2];

        cout<<ans<<endl;

    }



    //main();





    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.3917321872868247x):
#include <iostream> 

using namespace std;



int sushu[100001]={0};//素数表 

int qianzhui[100001]={0};//前缀和 

void sieve(){

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

		if(sushu[i]==0){//合数的倍数不需要再做筛选 

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

			    //要把合数下标置1

			    sushu[i*j]=1;

		    }

		}

	}

}

int main(){

	int q,l,r;

	cin>>q;

	sieve();

	sushu[1]=1;

	sushu[2]=1;

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

		if(sushu[i]==0&&sushu[(i+1)/2]==0&&i!=2||i==3){

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

		}else{

			qianzhui[i]=qianzhui[i-1];

		}

	}

	for(int j=1;j<=q;j++){

		cin>>l>>r;

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

	} 

    return 0;

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

using namespace std;

vector <bool> prim;

vector <int> vec;

vector <int>::iterator it;

vector <int>::iterator it1;

void prime()

{

    for(int i=0; i<100005; i++)

        prim.push_back(true);

    for(int i=2; i<100005; i++)

    {

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

            prim[j]=false;

    }

    for(int i=3; i<100005; i+=2)

    {

        int x=(i+1)/2;

        if(prim[i] == true && prim[x] == true)

        {

            vec.push_back(i);

            //cout<<"not"<<endl;

        }



    }

}

int main()

{

    prime();

    //cout<<vec[0]<<endl;

    int q;

    int l,r;

    cin>>q;

    while(q--)

    {

        cin>>l>>r;

        it = lower_bound(vec.begin(),vec.end(),l);

        it1 = upper_bound(vec.begin(),vec.end(),r);

        int pos1=it-vec.begin();

        //cout<<pos1<<endl;

        int pos2 = it1-vec.begin();

        //cout<<pos2<<endl;

        cout<<pos2-pos1<<endl;

    }

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.3078873718602033x):
#include <iostream> 

using namespace std;



int sushu[100001]={0};//素数表 

int qianzhui[100001]={0};//前缀和 

void sieve(){

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

		if(sushu[i]==0){//合数的倍数不需要再做筛选 

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

			    //要把合数下标置1

			    sushu[i*j]=1;

		    }

		}

	}

}

int main(){

	int q,l,r;

	cin>>q;

	sieve();

	sushu[1]=1;

	sushu[2]=1;

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

		if(sushu[i]==0&&sushu[(i+1)/2]==0&&i!=2||i==3){

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

		}else{

			qianzhui[i]=qianzhui[i-1];

		}

	}

	for(int j=1;j<=q;j++){

		cin>>l>>r;

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

	} 

    return 0;

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



using namespace std ;



const int MAX = 1e5 + 10 ;



int prime[MAX] , ans[MAX];



int main()

{

	ios_base::sync_with_stdio(0) ;

	cin.tie(0) ;

	for(int i = 0 ; i < MAX ; ++i)

		prime[i] = 1 ; 

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

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

	{

		if(prime[i])

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

				prime[j] = 0 ;

	}

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

	{

		ans[i] = ans[i-1] + (bool)(prime[i] && prime[(i+1)/2]);

	}

	int q ;

	cin>>q ;

	while(q--)

	{

		int l , r ;

		cin>>l>>r ;

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

	}

	return 0 ;

}

// Faster C++ solution for the same problem (measured speedup: 1.1990919162834819x):
#include <iostream> 

using namespace std;



int sushu[100001]={0};//素数表 

int qianzhui[100001]={0};//前缀和 

void sieve(){

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

		if(sushu[i]==0){//合数的倍数不需要再做筛选 

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

			    //要把合数下标置1

			    sushu[i*j]=1;

		    }

		}

	}

}

int main(){

	int q,l,r;

	cin>>q;

	sieve();

	sushu[1]=1;

	sushu[2]=1;

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

		if(sushu[i]==0&&sushu[(i+1)/2]==0&&i!=2||i==3){

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

		}else{

			qianzhui[i]=qianzhui[i-1];

		}

	}

	for(int j=1;j<=q;j++){

		cin>>l>>r;

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

	} 

    return 0;

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

using namespace std;

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

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

#define FOR(i, m, n) for(int i = m; i <= n; i++)

#define FORR(i, m, n) for(int i = m; i >= n; i--)

#define SORT(v, n) sort(v, v+n);

#define VSORT(v) sort(v.begin(), v.end());

 

using ll = long long;

using vll = vector<ll>;

using vvll = vector<vector<ll>>;

using P = pair<ll, ll>;



vll Eratosthenes(ll N){

    vll a(N + 1, 1);

  	for(ll i = 0; i < N; i++){

		a[i] = 1;

	}

	for(ll i = 2; i < sqrt(N); i++){

		if(a[i]){

			for(ll j = 0; i * (j + 2) < N; j++){

				a[i *(j + 2)] = 0;

			}

		}

	}

    a[0] = a[1] = 0;

    return a;

}



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);



    ll q;

    cin >> q;

    ll l, r;



    vll isprime = Eratosthenes(1e5 + 10);

    vll sss(1e5 + 11);

    REP(i, 1e5 + 10){

        if(i == 0) continue;

        sss[i] = sss[i - 1] + isprime[i] * isprime[(i + 1) / 2];

    }

    while(q){

        cin >> l >> r;

        ll res = sss[r] - sss[l - 1];

        cout << res << endl;

        q--;

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.6048702968418695x):
#include <iostream> 

using namespace std;



int sushu[100001]={0};//素数表 

int qianzhui[100001]={0};//前缀和 

void sieve(){

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

		if(sushu[i]==0){//合数的倍数不需要再做筛选 

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

			    //要把合数下标置1

			    sushu[i*j]=1;

		    }

		}

	}

}

int main(){

	int q,l,r;

	cin>>q;

	sieve();

	sushu[1]=1;

	sushu[2]=1;

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

		if(sushu[i]==0&&sushu[(i+1)/2]==0&&i!=2||i==3){

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

		}else{

			qianzhui[i]=qianzhui[i-1];

		}

	}

	for(int j=1;j<=q;j++){

		cin>>l>>r;

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

	} 

    return 0;

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

using namespace std;

int const N=1e5;

int prime[N];

int ans[N];

int main() {

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

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

        prime[i]=1;

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

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

{

    if(prime[i]==1)

    {

        //cout<<i;

        for(long long  j=i*i;j<=N;j+=i)

        {



            //cout<<j;

         prime[j]=0;

        }

    }

}



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

{

   int  x=(i+1)/2;

    if(prime[i]&&prime[x])

    {

       // prime[i]=1;

       ans[i]=1;

   // cout<<"yes";

    }

    else

        ans[i]=0;

   // cout<<i;

}



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

{

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

}



int q;

cin>>q;

while(q--)

{

    int l,r;

    cin>>l>>r;



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

}

}


// Faster C++ solution for the same problem (measured speedup: 1.138444372078862x):
#include <iostream> 

using namespace std;



int sushu[100001]={0};//素数表 

int qianzhui[100001]={0};//前缀和 

void sieve(){

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

		if(sushu[i]==0){//合数的倍数不需要再做筛选 

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

			    //要把合数下标置1

			    sushu[i*j]=1;

		    }

		}

	}

}

int main(){

	int q,l,r;

	cin>>q;

	sieve();

	sushu[1]=1;

	sushu[2]=1;

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

		if(sushu[i]==0&&sushu[(i+1)/2]==0&&i!=2||i==3){

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

		}else{

			qianzhui[i]=qianzhui[i-1];

		}

	}

	for(int j=1;j<=q;j++){

		cin>>l>>r;

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

	} 

    return 0;

}
// Slower C++ solution:
#include <iostream>

#define MAXNUM 100000

using namespace std;

bool f[MAXNUM + 1];

int c[MAXNUM + 1];

int main(void) {

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

    if (!f[i]) {

      for (int j = i + i; j <= MAXNUM; j += i) {

        f[j] = 1;

      }

    }

  }

  for (int i = 3; i <= MAXNUM; i++) {

    if (!f[i] && !f[(i + 1) / 2]) c[i] = 1;

  }

  for (int i = 3; i <= MAXNUM; i++) {

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

  }

  int q;

  cin >> q;

  for (int i = 0; i < q; i++) {

    int l, r;

    cin >> l >> r;

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

  }

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.221674861684763x):
#include <iostream>

#include <vector>

#define MAX_R 100000

using namespace std;

int main(void) {

  int q;

  cin >> q;

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

  for (int i = 0; i < q; i++) {

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

  }

  vector<bool> prime(MAX_R + 1, 1);

  vector<int> count(MAX_R + 1, 0);

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

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

    if (prime[i]) {

      for (int j = i * i; j <= MAX_R; j += i) {

        prime[j] = 0;

      }

    }

  }

  for (int i = 3; i <= MAX_R; i++) {

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

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

    } else {

      count[i] = count[i - 1];

    }

  }

  for (int i = 0; i < q; i++) {

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

  }

  return 0;

}
// Slower C++ solution:
#include <iostream>

#include <vector>

#define MAX_R 100000

using namespace std;

int main(void) {

  int q;

  cin >> q;

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

  for (int i = 0; i < q; i++) {

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

  }

  vector<bool> prime(MAX_R + 1, 1);

  vector<int> count(MAX_R + 1, 0);

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

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

    if (prime[i]) {

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

        prime[j] = 0;

      }

    }

  }

  for (int i = 3; i <= MAX_R; i++) {

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

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

    } else {

      count[i] = count[i - 1];

    }

  }

  for (int i = 0; i < q; i++) {

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

  }

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1397030225790818x):
#include <iostream>

#include <vector>

#define MAX_R 100000

using namespace std;

int main(void) {

  int q;

  cin >> q;

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

  for (int i = 0; i < q; i++) {

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

  }

  vector<bool> prime(MAX_R + 1, 1);

  vector<int> count(MAX_R + 1, 0);

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

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

    if (prime[i]) {

      for (int j = i * i; j <= MAX_R; j += i) {

        prime[j] = 0;

      }

    }

  }

  for (int i = 3; i <= MAX_R; i++) {

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

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

    } else {

      count[i] = count[i - 1];

    }

  }

  for (int i = 0; i < q; i++) {

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

  }

  return 0;

}
// Slower C++ solution:
#include <iostream>

#include <vector>

#define MAX_R 100000

using namespace std;

int main(void) {

  int q;

  cin >> q;

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

  for (int i = 0; i < q; i++) {

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

  }

  vector<bool> prime(MAX_R + 1, 1);

  vector<int> count(MAX_R + 1, 0);

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

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

    if (prime[i]) {

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

        prime[j] = 0;

      }

    }

  }

  for (int i = 3; i <= MAX_R; i++) {

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

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

    } else {

      count[i] = count[i - 1];

    }

  }

  for (int i = 0; i < q; i++) {

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

  }

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1525691565758087x):
#include <iostream>

#include <vector>

#define MAX_R 100000

using namespace std;

int main(void) {

  int q;

  cin >> q;

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

  for (int i = 0; i < q; i++) {

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

  }

  vector<bool> prime(MAX_R + 1, 1);

  vector<int> count(MAX_R + 1, 0);

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

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

    if (prime[i]) {

      for (int j = i * i; j <= MAX_R; j += i) {

        prime[j] = 0;

      }

    }

  }

  for (int i = 3; i <= MAX_R; i++) {

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

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

    } else {

      count[i] = count[i - 1];

    }

  }

  for (int i = 0; i < q; i++) {

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

  }

  return 0;

}
// Slower C++ solution:
#include <iostream>

#define MAXNUM 100000

using namespace std;

bool f[MAXNUM + 1];

int c[MAXNUM + 1];

int main(void) {

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

    if (!f[i]) {

      for (int j = i + i; j <= MAXNUM; j += i) {

        f[j] = 1;

      }

    }

  }

  for (int i = 3; i <= MAXNUM; i++) {

    if (!f[i] && !f[(i + 1) / 2]) c[i] = 1;

  }

  for (int i = 3; i <= MAXNUM; i++) {

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

  }

  int q;

  cin >> q;

  for (int i = 0; i < q; i++) {

    int l, r;

    cin >> l >> r;

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

  }

  return 0;

}

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





#define dhai_machli ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define endl "\n"

#define ll long long

#define pii pair<int,int>

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

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

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

#define brep(i,a,b,c) for(int i=a; i>b; i-=c)

#define tc(t) int t; cin>>t; while(t--)

#define sz(v) int((v).size())

#define pb push_back



const int N = 2e5 + 10;





bool f[100001];

int c[100002];



int n, l, r;



using namespace std;



int main()

{







   dhai_machli;







   for ( int i = 2; i * i <= 100000; i ++) if (! f [ i ]) 

      for ( int j = i * i ; j <= 100000; j += i )

            f[ j ] = true ;

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



      if (! f [ i ] && ! f [( i + 1) / 2]) c [ i ]++;



   for ( int i = 3; i <= 100000; i ++) c [ i ] += c [i - 1];



      

   cin >> n;

   while (n--)

   {

      cin >> l >> r;



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

   }





   return 0;

}