Q = int(eval(input()))

qs = [tuple(map(int,input().split())) for i in range(Q)]



MAXN = 10**5+10

sieve = [0,0] + [1]*MAXN

p = 2

while p*p <= MAXN:

    if sieve[p]:

        for q in range(2*p,MAXN+1,p):

            sieve[q] = 0

    p += 1



cums = [0]

for i in range(1,MAXN):

    valid = sieve[i] and sieve[(i+1)//2]

    cums.append(cums[-1] + int(valid))



for l,r in qs:

    ans = cums[r] - cums[l-1]

    print(ans)
Q = int(input())

LR = [tuple(map(int,input().split())) for i in range(Q)]



MAXN = 10**5+10

sieve = [0,0] + [1]*MAXN

p = 2

while p*p <= MAXN:

    if sieve[p]:

        for q in range(2*p,MAXN+1,p):

            sieve[q] = 0

    p += 1



likes = [0] * MAXN

for i in range(3,MAXN,2):

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

        likes[i] += 1

cl = [0]

for l in likes:

    cl.append(l + cl[-1])



ans = []

for l,r in LR:

    ans.append(cl[r+1] - cl[l])

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

Q = int(eval(input()))
qs = [tuple(map(int, input().split())) for i in range(Q)]
MAXN = 10**5 + 10
sieve = [0, 0] + [1] * MAXN
p = 2
while p * p <= MAXN:
    if sieve[p]:
        for q in range(2 * p, MAXN + 1, p):
            sieve[q] = 0
    p += 1
cums = [0]
for i in range(1, MAXN):
    valid = sieve[i] and sieve[(i + 1) // 2]
    cums.append(cums[-1] + int(valid))
for l, r in qs:
    ans = cums[r] - cums[l - 1]
    print(ans)

Q = int(input())
LR = [tuple(map(int, input().split())) for i in range(Q)]
MAXN = 10**5 + 10
sieve = [0, 0] + [1] * MAXN
p = 2
while p * p <= MAXN:
    if sieve[p]:
        for q in range(2 * p, MAXN + 1, p):
            sieve[q] = 0
    p += 1
likes = [0] * MAXN
for i in range(3, MAXN, 2):
    if sieve[i] and sieve[(i + 1) // 2]:
        likes[i] += 1
cl = [0]
for l in likes:
    cl.append(l + cl[-1])
ans = []
for l, r in LR:
    ans.append(cl[r + 1] - cl[l])
print(*ans, sep="\n")

-Q = int(eval(input())) -qs = [tuple(map(int, input().split())) for i in range(Q)] +Q = int(input()) +LR = [tuple(map(int, input().split())) for i in range(Q)] -cums = [0] -for i in range(1, MAXN): -    valid = sieve[i] and sieve[(i + 1) // 2] -    cums.append(cums[-1] + int(valid)) -for l, r in qs: -    ans = cums[r] - cums[l - 1] -    print(ans) +likes = [0] * MAXN +for i in range(3, MAXN, 2): +    if sieve[i] and sieve[(i + 1) // 2]: +        likes[i] += 1 +cl = [0] +for l in likes: +    cl.append(l + cl[-1]) +ans = [] +for l, r in LR: +    ans.append(cl[r + 1] - cl[l]) +print(*ans, sep="\n")