def isPrime(x):

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

    if x%i==0:

      return False

  return True



q=int(eval(input()))

f=[1]*(10**5+1)

f[0],f[1]=0,0

n=len(f)

for i in range(2,n):

  if isPrime(i):

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

      f[j]=0



c=[0]*(10**5+1)

for i in range(2,10**5+1):

  if i%2==1:

    if f[i]==1 and f[(i+1)//2]==1:

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

      continue

    c[i]=c[i-1]

  else:

    c[i]=c[i-1]

for i in range(q) :

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

   print((c[r]-c[l-1]))
from itertools import accumulate

import sys

input = sys.stdin.buffer.readline





def sieve(n):

    f = []

    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

    for i in range(n+1):

        if is_prime[i]:

            f.append(i)

    return f





s = sieve(10**5)

s = set(s)

number_2017 = [0]*(10**5+1)



for i in range(1, 10**5+1):

    if i % 2 == 0:

        continue

    if i in s and (i+1)//2 in s:

        number_2017[i] = 1





acc = list(accumulate(number_2017))



Q = int(eval(input()))

for _ in range(Q):

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

    print((acc[r]-acc[l-1]))

def isPrime(x):
    for i in range(2, int(x**0.5) + 1):
        if x % i == 0:
            return False
    return True


q = int(eval(input()))
f = [1] * (10**5 + 1)
f[0], f[1] = 0, 0
n = len(f)
for i in range(2, n):
    if isPrime(i):
        for j in range(i * 2, n, i):
            f[j] = 0
c = [0] * (10**5 + 1)
for i in range(2, 10**5 + 1):
    if i % 2 == 1:
        if f[i] == 1 and f[(i + 1) // 2] == 1:
            c[i] = c[i - 1] + 1
            continue
        c[i] = c[i - 1]
    else:
        c[i] = c[i - 1]
for i in range(q):
    l, r = list(map(int, input().split()))
    print((c[r] - c[l - 1]))

from itertools import accumulate
import sys

input = sys.stdin.buffer.readline


def sieve(n):
    f = []
    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
    for i in range(n + 1):
        if is_prime[i]:
            f.append(i)
    return f


s = sieve(10**5)
s = set(s)
number_2017 = [0] * (10**5 + 1)
for i in range(1, 10**5 + 1):
    if i % 2 == 0:
        continue
    if i in s and (i + 1) // 2 in s:
        number_2017[i] = 1
acc = list(accumulate(number_2017))
Q = int(eval(input()))
for _ in range(Q):
    l, r = list(map(int, input().split()))
    print((acc[r] - acc[l - 1]))

-def isPrime(x): -    for i in range(2, int(x**0.5) + 1): -        if x % i == 0: -            return False -    return True +from itertools import accumulate +import sys + +input = sys.stdin.buffer.readline -q = int(eval(input())) -f = [1] * (10**5 + 1) -f[0], f[1] = 0, 0 -n = len(f) -for i in range(2, n): -    if isPrime(i): -        for j in range(i * 2, n, i): -            f[j] = 0 -c = [0] * (10**5 + 1) -for i in range(2, 10**5 + 1): -    if i % 2 == 1: -        if f[i] == 1 and f[(i + 1) // 2] == 1: -            c[i] = c[i - 1] + 1 +def sieve(n): +    f = [] +    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]: -        c[i] = c[i - 1] -    else: -        c[i] = c[i - 1] -for i in range(q): +        for j in range(i * 2, n + 1, i): +            is_prime[j] = False +    for i in range(n + 1): +        if is_prime[i]: +            f.append(i) +    return f + + +s = sieve(10**5) +s = set(s) +number_2017 = [0] * (10**5 + 1) +for i in range(1, 10**5 + 1): +    if i % 2 == 0: +        continue +    if i in s and (i + 1) // 2 in s: +        number_2017[i] = 1 +acc = list(accumulate(number_2017)) +Q = int(eval(input())) +for _ in range(Q): -    print((c[r] - c[l - 1])) +    print((acc[r] - acc[l - 1]))