import math

from itertools import accumulate



def is_prime(n):

  if n == 1: return False

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

    if n % k == 0: return False

  return True



MAX = (10**5)+1

q = int(eval(input()))

A = [1 if is_prime(i)and is_prime((i + 1)//2) else 0 for i in range(MAX) ]

S = [0] + list(accumulate(A))



Q =[]

Q_append = Q.append

for _ in range(q):

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

    Q_append((l, r))



for qi in Q:  

    print((S[qi[1]+1]-S[qi[0]]))
from itertools import accumulate



def primes(n):

  prime = ([False]*2) + ([True]*(n-2))

  for i in range(2, n):

    if prime[i]:

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

        prime[j] = False

  return prime



MAX = (10**5)+1

is_prime = primes(MAX)

q = int(eval(input()))

A = [1 if is_prime[i] and is_prime[(i + 1)//2] else 0 for i in range(MAX)]

S = [0] + list(accumulate(A))



Q = []

Q_append = Q.append

for _ in range(q):

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

    Q_append((l, r))



for qi in Q:

    print((S[qi[1]+1]-S[qi[0]]))
import math
from itertools import accumulate


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


MAX = (10**5) + 1
q = int(eval(input()))
A = [1 if is_prime(i) and is_prime((i + 1) // 2) else 0 for i in range(MAX)]
S = [0] + list(accumulate(A))
Q = []
Q_append = Q.append
for _ in range(q):
    l, r = list(map(int, input().split()))
    Q_append((l, r))
for qi in Q:
    print((S[qi[1] + 1] - S[qi[0]]))

from itertools import accumulate


def primes(n):
    prime = ([False] * 2) + ([True] * (n - 2))
    for i in range(2, n):
        if prime[i]:
            for j in range(i * 2, n, i):
                prime[j] = False
    return prime


MAX = (10**5) + 1
is_prime = primes(MAX)
q = int(eval(input()))
A = [1 if is_prime[i] and is_prime[(i + 1) // 2] else 0 for i in range(MAX)]
S = [0] + list(accumulate(A))
Q = []
Q_append = Q.append
for _ in range(q):
    l, r = list(map(int, input().split()))
    Q_append((l, r))
for qi in Q:
    print((S[qi[1] + 1] - S[qi[0]]))

-import math -def is_prime(n): -    if n == 1: -        return False -    for k in range(2, int(math.sqrt(n)) + 1): -        if n % k == 0: -            return False -    return True +def primes(n): +    prime = ([False] * 2) + ([True] * (n - 2)) +    for i in range(2, n): +        if prime[i]: +            for j in range(i * 2, n, i): +                prime[j] = False +    return prime +is_prime = primes(MAX) -A = [1 if is_prime(i) and is_prime((i + 1) // 2) else 0 for i in range(MAX)] +A = [1 if is_prime[i] and is_prime[(i + 1) // 2] else 0 for i in range(MAX)]