#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
template<class S, class T> inline S min_L(S a,T b){
  return a<=b?a:b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16]={0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T> void malloc1d(T **arr, int x){
  (*arr) = (T*)malloc(x*sizeof(T));
}
template<class T> void free1d(T *arr){
  free(arr);
}
inline void rd(int &x){
  int k, m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(int x){
  char f[10];
  int m=0, s=0;
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
int Prime_L(int N, int res[], void *mem=wmem){
  bool *isprime;
  const int r=23000;
  int a, b, i, *sf, ss=1, sz=1;
  walloc1d(&isprime, r, &mem);
  walloc1d(&sf, r, &mem);
  isprime = (bool*)mem;
  sf = (int*)(isprime + r);
  N /= 2;
  res[0] = 2;
  b =min_L(r, N);
  for(i=(1);i<(b);i++){
    isprime[i] = 1;
  }
  for(i=(1);i<(b);i++){
    if(isprime[i]){
      res[sz++] = 2*i+1;
      sf[ss] = 2*i*(i+1);
      if(sf[ss] < N){
        while(sf[ss] < r){
          isprime[sf[ss]] = 0;
          sf[ss] += res[ss];
        }
        ss++;
      }
    }
  }
  for(a=r; a<N; a+=r){
    b =min_L(a + r, N);
    isprime -= r;
    for(i=(a);i<(b);i++){
      isprime[i] = 1;
    }
    for(i=(1);i<(ss);i++){
      while(sf[i] < b){
        isprime[sf[i]] = 0;
        sf[i] += res[i];
      }
    }
    for(i=(a);i<(b);i++){
      if(isprime[i]){
        res[sz++] = 2*i+1;
      }
    }
  }
  return sz;
}
template<class T> struct Grid1d{
  T *d, *d_s;
  int *dw, *lf, n, *rg, set_d, set_s, *up;
  void malloc(const int nn){
    n = nn;
    set_s = 0;
    set_d = 0;
    malloc1d(&d, n);
  }
  void free(void){
    free1d(d);
    if(set_s){
      free1d(d_s);
    }
    if(set_d){
      free1d(up);
      free1d(dw);
    }
  }
  T& operator[](int a){
    return d[a];
  }
  void setSum(void){
    int i;
    if(set_s == 0){
      set_s = 1;
      malloc1d(&d_s, n+1);
    }
    d_s[0] = 0;
    for(i=0;i<(n);i++){
      d_s[i+1] = d_s[i] + d[i];
    }
  }
  void setDir(void){
    int i;
    if(set_d == 0){
      set_d = 1;
      malloc1d(&up, n);
      malloc1d(&dw, n);
      lf = dw;
      rg = up;
    }
    lf[0] = 1;
    for(i=(1);i<(n);i++){
      if(d[i]==d[i-1]){
        lf[i] = 1 + lf[i-1];
      }
      else{
        lf[i] = 1 ;
      }
    }
    rg[n-1] = 1;
    for(i=n-2;i>=0;i--){
      if(d[i]==d[i+1]){
        rg[i] = 1 + rg[i+1];
      }
      else{
        rg[i] = 1 ;
      }
    }
  }
  void setDirMatch(const T v){
    int i;
    if(set_d == 0){
      set_d = 1;
      malloc1d(&up, n);
      malloc1d(&dw, n);
      lf = dw;
      rg = up;
    }
    if(d[0]==v){
      lf[0] =1;
    }
    else{
      lf[0] =0;
    }
    for(i=(1);i<(n);i++){
      if(d[i]==v){
        lf[i] =1 + lf[i-1];
      }
      else{
        lf[i] =0;
      }
    }
    if(d[n-1]==v){
      rg[n-1] =1;
    }
    else{
      rg[n-1] =0;
    }
    for(i=n-2;i>=0;i--){
      if(d[i]==v){
        rg[i] =1 + rg[i+1];
      }
      else{
        rg[i] =0;
      }
    }
  }
  inline T getSum(const int a, const int b){
    return d_s[b+1] - d_s[a];
  }
}
;
char memarr[96000000];
int Q;
int L;
int R;
int ps;
int p[9592];
int isp[100001];
int main(){
  Grid1d<int> g;
  int Lj4PdHRW, i, res;
  wmem = memarr;
  ps =Prime_L(100001, p);
  for(i=0;i<(ps);i++){
    isp[p[i]] = 1;
  }
  g.malloc(100001);
  for(i=0;i<(100001);i++){
    g[i] = 0;
  }
  for(i=1;i<=100000;i+=2){
    if(isp[i] && isp[(i+1)/2]){
      g[i] = 1;
    }
  }
  g.setSum();
  rd(Q);
  for(Lj4PdHRW=0;Lj4PdHRW<(Q);Lj4PdHRW++){
    rd(L);
    rd(R);
    res = g.getSum(L,R);
    wt_L(res);
    wt_L('\n');
  }
  return 0;
}
// cLay varsion 20190827-1

// --- original code ---
// int Q, L, R;
// int ps, p[9592], isp[100001];
// {
//   int i, res;
//   Grid1d<int> g;
//   
//   ps = Prime(100001, p);
//   rep(i,ps) isp[p[i]] = 1;
// 
//   g.malloc(100001);
//   rep(i,100001) g[i] = 0;
//   for(i=1;i<=1d5;i+=2) if(isp[i] && isp[(i+1)/2]) g[i] = 1;
//   g.setSum();
// 
//   rd(Q);
//   rep(Q){
//     rd(L, R);
//     res = g.getSum(L,R);
//     wt(res);
//   }
// }


Measured memory_bytes: 1920