q=int(eval(input()))

P2017=[]

def primes(n):

    is_prime = [True] * (n + 1)

    is_prime[0] = False

    is_prime[1] = False

    for i in range(2, int(n**0.5) + 1):

        if not is_prime[i]:

            continue

        for j in range(i * 2, n + 1, i):

            is_prime[j] = False

    return [i for i in range(n + 1) if is_prime[i]]

P=primes(10**5)

for p in P:

  if (p+1)//2 in P:

    P2017.append(p)

LR=[0]*(10**5)

for p in P2017:

  LR[p]+=1

for i in range(10**5-1):

  LR[i+1]+=LR[i]

for i in range(q):

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

  print((LR[r]-LR[l-1]))
import sys

input=lambda: sys.stdin.readline().rstrip()

n=10**5

is_prime=[True]*(n+1)

is_prime[0]=False

is_prime[1]=False

for i in range(2,int(n**0.5)+1):

  if not is_prime[i]:

    continue

  for j in range(i*2,n+1,i):

    is_prime[j]=False

A=[0]*n

def f(x):

  return is_prime[x] and is_prime[(x+1)//2]



for i in range(3,n,2):

  if f(i):

    A[i]=A[i-2]+1

  else:

    A[i]=A[i-2]

A=[0]+A

q=int(eval(input()))

for _ in range(q):

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

  print((A[r+1]-A[l-1]))

q = int(eval(input()))
P2017 = []


def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]


P = primes(10**5)
for p in P:
    if (p + 1) // 2 in P:
        P2017.append(p)
LR = [0] * (10**5)
for p in P2017:
    LR[p] += 1
for i in range(10**5 - 1):
    LR[i + 1] += LR[i]
for i in range(q):
    l, r = list(map(int, input().split()))
    print((LR[r] - LR[l - 1]))

import sys

input = lambda: sys.stdin.readline().rstrip()
n = 10**5
is_prime = [True] * (n + 1)
is_prime[0] = False
is_prime[1] = False
for i in range(2, int(n**0.5) + 1):
    if not is_prime[i]:
        continue
    for j in range(i * 2, n + 1, i):
        is_prime[j] = False
A = [0] * n


def f(x):
    return is_prime[x] and is_prime[(x + 1) // 2]


for i in range(3, n, 2):
    if f(i):
        A[i] = A[i - 2] + 1
    else:
        A[i] = A[i - 2]
A = [0] + A
q = int(eval(input()))
for _ in range(q):
    l, r = list(map(int, input().split()))
    print((A[r + 1] - A[l - 1]))

-q = int(eval(input())) -P2017 = [] +import sys + +input = lambda: sys.stdin.readline().rstrip() +n = 10**5 +is_prime = [True] * (n + 1) +is_prime[0] = False +is_prime[1] = False +for i in range(2, int(n**0.5) + 1): +    if not is_prime[i]: +        continue +    for j in range(i * 2, n + 1, i): +        is_prime[j] = False +A = [0] * n -def primes(n): -    is_prime = [True] * (n + 1) -    is_prime[0] = False -    is_prime[1] = False -    for i in range(2, int(n**0.5) + 1): -        if not is_prime[i]: -            continue -        for j in range(i * 2, n + 1, i): -            is_prime[j] = False -    return [i for i in range(n + 1) if is_prime[i]] +def f(x): +    return is_prime[x] and is_prime[(x + 1) // 2] -P = primes(10**5) -for p in P: -    if (p + 1) // 2 in P: -        P2017.append(p) -LR = [0] * (10**5) -for p in P2017: -    LR[p] += 1 -for i in range(10**5 - 1): -    LR[i + 1] += LR[i] -for i in range(q): +for i in range(3, n, 2): +    if f(i): +        A[i] = A[i - 2] + 1 +    else: +        A[i] = A[i - 2] +A = [0] + A +q = int(eval(input())) +for _ in range(q): -    print((LR[r] - LR[l - 1])) +    print((A[r + 1] - A[l - 1]))