#素数判定

import math

from itertools import accumulate

def sosuu(n):

  if n==1:

    return False

  else:

    for i in range(2,int(math.sqrt(n)+1)):

      if n%i==0:

        return False

    return True



#入力

q=int(eval(input()))

l=[0]*q

r=[0]*q

for i in range(q):

    l[i],r[i]=list(map(int,input().split()))

L=min(l)

R=max(r)



#累積和を作る

A=[]

for i in range(L,R+1):

  if i%2==0:

    A.append(0)

  else:

    if sosuu(i) and sosuu(int((i+1)/2)):

      A.append(1)

    else:

      A.append(0)

s=[0]+A

S=list(accumulate(s))

for i in range(q):

    #print(S[r[i]-L+1])

    #print(S[l[i]-L])

    print((S[r[i]-L+1]-S[l[i]-L]))

    



import math

from itertools import accumulate

q=int(eval(input()))

l=[]

r=[]

for i in range(q):

  a,b=list(map(int,input().split()))

  l.append(a)

  r.append(b)

  

def is_prime(n):

  if n==1:

    return False

  elif n==2:

    return True

  else:

    for i in range(2,int(math.sqrt(n)+1)):

      if n%i==0:

        return False

  return True



l_min=min(l)

r_max=max(r)

ruiseki=[]

for i in range(l_min,r_max+1):

  if i%2==1 and is_prime(i) and is_prime((i+1)//2):

    ruiseki.append(1)

  else:

    ruiseki.append(0)

    

ruiseki=[0]+list(accumulate(ruiseki))



for i in range(q):

  print((ruiseki[r[i]-l_min+1]-ruiseki[l[i]-l_min]))

  

    

    

  

# 素数判定
import math
from itertools import accumulate


def sosuu(n):
    if n == 1:
        return False
    else:
        for i in range(2, int(math.sqrt(n) + 1)):
            if n % i == 0:
                return False
        return True


# 入力
q = int(eval(input()))
l = [0] * q
r = [0] * q
for i in range(q):
    l[i], r[i] = list(map(int, input().split()))
L = min(l)
R = max(r)
# 累積和を作る
A = []
for i in range(L, R + 1):
    if i % 2 == 0:
        A.append(0)
    else:
        if sosuu(i) and sosuu(int((i + 1) / 2)):
            A.append(1)
        else:
            A.append(0)
s = [0] + A
S = list(accumulate(s))
for i in range(q):
    # print(S[r[i]-L+1])
    # print(S[l[i]-L])
    print((S[r[i] - L + 1] - S[l[i] - L]))

import math
from itertools import accumulate

q = int(eval(input()))
l = []
r = []
for i in range(q):
    a, b = list(map(int, input().split()))
    l.append(a)
    r.append(b)


def is_prime(n):
    if n == 1:
        return False
    elif n == 2:
        return True
    else:
        for i in range(2, int(math.sqrt(n) + 1)):
            if n % i == 0:
                return False
    return True


l_min = min(l)
r_max = max(r)
ruiseki = []
for i in range(l_min, r_max + 1):
    if i % 2 == 1 and is_prime(i) and is_prime((i + 1) // 2):
        ruiseki.append(1)
    else:
        ruiseki.append(0)
ruiseki = [0] + list(accumulate(ruiseki))
for i in range(q):
    print((ruiseki[r[i] - l_min + 1] - ruiseki[l[i] - l_min]))

-# 素数判定 +q = int(eval(input())) +l = [] +r = [] +for i in range(q): +    a, b = list(map(int, input().split())) +    l.append(a) +    r.append(b) -def sosuu(n): + +def is_prime(n): +    elif n == 2: +        return True -        return True +    return True -# 入力 -q = int(eval(input())) -l = [0] * q -r = [0] * q +l_min = min(l) +r_max = max(r) +ruiseki = [] +for i in range(l_min, r_max + 1): +    if i % 2 == 1 and is_prime(i) and is_prime((i + 1) // 2): +        ruiseki.append(1) +    else: +        ruiseki.append(0) +ruiseki = [0] + list(accumulate(ruiseki)) -    l[i], r[i] = list(map(int, input().split())) -L = min(l) -R = max(r) -# 累積和を作る -A = [] -for i in range(L, R + 1): -    if i % 2 == 0: -        A.append(0) -    else: -        if sosuu(i) and sosuu(int((i + 1) / 2)): -            A.append(1) -        else: -            A.append(0) -s = [0] + A -S = list(accumulate(s)) -for i in range(q): -    # print(S[r[i]-L+1]) -    # print(S[l[i]-L]) -    print((S[r[i] - L + 1] - S[l[i] - L])) +    print((ruiseki[r[i] - l_min + 1] - ruiseki[l[i] - l_min]))