from itertools import accumulate



# ある自然数未満までのエラトステネスの篩をつくる

def mark(s, x):

    for i in range(x + x, len(s), x):

        s[i] = False





def eratosthenes(n):

    s = [True] * n

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

        if s[x]: mark(s, x)

    return [i for i in range(0, n) if s[i] and i > 1]





Prime = set(eratosthenes(10**5 + 1))

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

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

    if n in Prime and (n + 1) / 2 in Prime:

        Target[n] = 1



Target = list(accumulate(Target))



Q = int(eval(input()))

for q in range(Q):

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

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

from itertools import accumulate





# ある自然数未満までのエラトステネスの篩をつくる

def mark(s, x):

    for i in range(x + x, len(s), x):

        s[i] = False





def eratosthenes(n):

    s = [True] * n

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

        if s[x]: mark(s, x)

    return [i for i in range(0, n) if s[i] and i > 1]





E = eratosthenes(10 ** 5 + 1)

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

for e in E:

    P[e] = 1



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

for n in range(10 ** 5 + 1):

    if ((n + 1) / 2).is_integer():

        if P[n] and P[(n + 1) // 2]:

            like2017[n] = 1

like2017 = list(accumulate(like2017))



Q = int(eval(input()))

for i in range(Q):

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

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

from itertools import accumulate

# ある自然数未満までのエラトステネスの篩をつくる
def mark(s, x):
    for i in range(x + x, len(s), x):
        s[i] = False


def eratosthenes(n):
    s = [True] * n
    for x in range(2, int(n**0.5) + 1):
        if s[x]:
            mark(s, x)
    return [i for i in range(0, n) if s[i] and i > 1]


Prime = set(eratosthenes(10**5 + 1))
Target = [0] * (10**5 + 1)
for n in range(1, 10**5 + 1):
    if n in Prime and (n + 1) / 2 in Prime:
        Target[n] = 1
Target = list(accumulate(Target))
Q = int(eval(input()))
for q in range(Q):
    l, r = list(map(int, input().split()))
    print((Target[r] - Target[l - 1]))

from itertools import accumulate

# ある自然数未満までのエラトステネスの篩をつくる
def mark(s, x):
    for i in range(x + x, len(s), x):
        s[i] = False


def eratosthenes(n):
    s = [True] * n
    for x in range(2, int(n**0.5) + 1):
        if s[x]:
            mark(s, x)
    return [i for i in range(0, n) if s[i] and i > 1]


E = eratosthenes(10**5 + 1)
P = [0] * (10**5 + 1)
for e in E:
    P[e] = 1
like2017 = [0] * (10**5 + 1)
for n in range(10**5 + 1):
    if ((n + 1) / 2).is_integer():
        if P[n] and P[(n + 1) // 2]:
            like2017[n] = 1
like2017 = list(accumulate(like2017))
Q = int(eval(input()))
for i in range(Q):
    l, r = list(map(int, input().split()))
    print((like2017[r] - like2017[l - 1]))

-Prime = set(eratosthenes(10**5 + 1)) -Target = [0] * (10**5 + 1) -for n in range(1, 10**5 + 1): -    if n in Prime and (n + 1) / 2 in Prime: -        Target[n] = 1 -Target = list(accumulate(Target)) +E = eratosthenes(10**5 + 1) +P = [0] * (10**5 + 1) +for e in E: +    P[e] = 1 +like2017 = [0] * (10**5 + 1) +for n in range(10**5 + 1): +    if ((n + 1) / 2).is_integer(): +        if P[n] and P[(n + 1) // 2]: +            like2017[n] = 1 +like2017 = list(accumulate(like2017)) -for q in range(Q): +for i in range(Q): -    print((Target[r] - Target[l - 1])) +    print((like2017[r] - like2017[l - 1]))