MAXN = 10**5+10

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

m = 2

while m*m <= MAXN:

    if sieve[m]:

        for n in range(2*m,MAXN,m):

            sieve[n] = 0

    m += 1



ok = [0]*MAXN

cums = [0]

for n in range(MAXN):

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

        ok[n] = 1

    cums.append(cums[-1] + ok[n])



Q = int(eval(input()))

for i in range(Q):

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

    print((cums[r+1] - cums[l]))

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)
MAXN = 10**5 + 10
sieve = [0, 0] + [1] * MAXN
m = 2
while m * m <= MAXN:
    if sieve[m]:
        for n in range(2 * m, MAXN, m):
            sieve[n] = 0
    m += 1
ok = [0] * MAXN
cums = [0]
for n in range(MAXN):
    if sieve[n] and sieve[(n + 1) // 2]:
        ok[n] = 1
    cums.append(cums[-1] + ok[n])
Q = int(eval(input()))
for i in range(Q):
    l, r = list(map(int, input().split()))
    print((cums[r + 1] - cums[l]))

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(eval(input())) +qs = [tuple(map(int, input().split())) for i in range(Q)] -m = 2 -while m * m <= MAXN: -    if sieve[m]: -        for n in range(2 * m, MAXN, m): -            sieve[n] = 0 -    m += 1 -ok = [0] * MAXN +p = 2 +while p * p <= MAXN: +    if sieve[p]: +        for q in range(2 * p, MAXN + 1, p): +            sieve[q] = 0 +    p += 1 -for n in range(MAXN): -    if sieve[n] and sieve[(n + 1) // 2]: -        ok[n] = 1 -    cums.append(cums[-1] + ok[n]) -Q = int(eval(input())) -for i in range(Q): -    l, r = list(map(int, input().split())) -    print((cums[r + 1] - cums[l])) +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)