

q = int(eval(input()))

lr = [list(map(int, input().split())) for _ in range(q)]

#nの素数判定

def is_prime(n):

    if n == 1:

        return 0

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

        if n % i == 0:

            return 0

    return 1



m = 0

for l, r in lr:

    m = max(l, r, m)

prime_count = [0, 0]

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

    prime_count += [is_prime(i)]

similar = [0]*(m+1)

for i in range(3, m+1):

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

        similar[i] = 1

for i in range(m):

    similar[i+1] = similar[i+1] + similar[i]



for l, r in lr:

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

input = sys.stdin.readline

Q = int(eval(input()))



#nの素数判定

def is_prime(n):

    if n == 1:

        return False

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

        if n % i == 0:

            return False

    return True



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

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

    if i%2 and is_prime(i) and is_prime((i+1)//2):

        l[i] = 1



for i in range(len(l)-1):

    l[i+1] += l[i]



for _ in range(Q):

    ll, rr = list(map(int, input().split()))

    print((l[rr]-l[ll-1]))
q = int(eval(input()))
lr = [list(map(int, input().split())) for _ in range(q)]
# nの素数判定
def is_prime(n):
    if n == 1:
        return 0
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return 0
    return 1


m = 0
for l, r in lr:
    m = max(l, r, m)
prime_count = [0, 0]
for i in range(2, m + 1):
    prime_count += [is_prime(i)]
similar = [0] * (m + 1)
for i in range(3, m + 1):
    if prime_count[i] == 1 and prime_count[(i + 1) // 2] == 1:
        similar[i] = 1
for i in range(m):
    similar[i + 1] = similar[i + 1] + similar[i]
for l, r in lr:
    print((similar[r] - similar[l - 1]))

import sys

input = sys.stdin.readline
Q = int(eval(input()))
# nの素数判定
def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True


l = [0] * (10**5 + 1)
for i in range(1, 10**5 + 1):
    if i % 2 and is_prime(i) and is_prime((i + 1) // 2):
        l[i] = 1
for i in range(len(l) - 1):
    l[i + 1] += l[i]
for _ in range(Q):
    ll, rr = list(map(int, input().split()))
    print((l[rr] - l[ll - 1]))

-q = int(eval(input())) -lr = [list(map(int, input().split())) for _ in range(q)] +import sys + +input = sys.stdin.readline +Q = int(eval(input())) -        return 0 +        return False -            return 0 -    return 1 +            return False +    return True -m = 0 -for l, r in lr: -    m = max(l, r, m) -prime_count = [0, 0] -for i in range(2, m + 1): -    prime_count += [is_prime(i)] -similar = [0] * (m + 1) -for i in range(3, m + 1): -    if prime_count[i] == 1 and prime_count[(i + 1) // 2] == 1: -        similar[i] = 1 -for i in range(m): -    similar[i + 1] = similar[i + 1] + similar[i] -for l, r in lr: -    print((similar[r] - similar[l - 1])) +l = [0] * (10**5 + 1) +for i in range(1, 10**5 + 1): +    if i % 2 and is_prime(i) and is_prime((i + 1) // 2): +        l[i] = 1 +for i in range(len(l) - 1): +    l[i + 1] += l[i] +for _ in range(Q): +    ll, rr = list(map(int, input().split())) +    print((l[rr] - l[ll - 1]))