from sys import stdin, setrecursionlimit

from itertools import accumulate



setrecursionlimit(10 ** 9)

INF = 1 << 60





def input():

    return stdin.readline().strip()





def prime_numbers(n):

    if n < 2:

        return []



    m = (n + 1) // 2

    p = [True] * m

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

        if p[i]:

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

                p[j] = False



    return {2} | {2 * i + 1 for i in range(1, m) if p[i]}





N = 10 ** 5

primes = prime_numbers(N)

a = [1 if n in primes and (n + 1) // 2 in primes else 0 for n in range(N + 1)]

a = list(accumulate(a))



Q, *LR = list(map(int, open(0).read().split()))

for l, r in zip(*[iter(LR)] * 2):

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

from sys import stdin, setrecursionlimit

from itertools import accumulate



setrecursionlimit(10 ** 9)

INF = 1 << 60





def input():

    return stdin.readline().strip()





def prime_numbers(n):

    if n < 2:

        return []



    m = (n + 1) // 2

    p = [True] * m

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

        if p[i]:

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

                p[j] = False



    return {2} | {2 * i + 1 for i in range(1, m) if p[i]}





N = 10 ** 5

primes = prime_numbers(N)

a = [1 if n in primes and (n + 1) // 2 in primes else 0 for n in range(N + 1)]

a = list(accumulate(a))



ans = []

Q, *LR = map(int, open(0).read().split())

for l, r in zip(*[iter(LR)] * 2):

    ans.append(a[r] - a[l - 1])



print(*ans, sep='\n')

from sys import stdin, setrecursionlimit
from itertools import accumulate

setrecursionlimit(10**9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


def prime_numbers(n):
    if n < 2:
        return []
    m = (n + 1) // 2
    p = [True] * m
    for i in range(1, int((n**0.5 - 1) / 2) + 1):
        if p[i]:
            for j in range(2 * i * (i + 1), m, 2 * i + 1):
                p[j] = False
    return {2} | {2 * i + 1 for i in range(1, m) if p[i]}


N = 10**5
primes = prime_numbers(N)
a = [1 if n in primes and (n + 1) // 2 in primes else 0 for n in range(N + 1)]
a = list(accumulate(a))
Q, *LR = list(map(int, open(0).read().split()))
for l, r in zip(*[iter(LR)] * 2):
    print((a[r] - a[l - 1]))

from sys import stdin, setrecursionlimit
from itertools import accumulate

setrecursionlimit(10**9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


def prime_numbers(n):
    if n < 2:
        return []
    m = (n + 1) // 2
    p = [True] * m
    for i in range(1, int((n**0.5 - 1) / 2) + 1):
        if p[i]:
            for j in range(2 * i * (i + 1), m, 2 * i + 1):
                p[j] = False
    return {2} | {2 * i + 1 for i in range(1, m) if p[i]}


N = 10**5
primes = prime_numbers(N)
a = [1 if n in primes and (n + 1) // 2 in primes else 0 for n in range(N + 1)]
a = list(accumulate(a))
ans = []
Q, *LR = map(int, open(0).read().split())
for l, r in zip(*[iter(LR)] * 2):
    ans.append(a[r] - a[l - 1])
print(*ans, sep="\n")

-Q, *LR = list(map(int, open(0).read().split())) +ans = [] +Q, *LR = map(int, open(0).read().split()) -    print((a[r] - a[l - 1])) +    ans.append(a[r] - a[l - 1]) +print(*ans, sep="\n")