import bisect



q = int(eval(input()))



def pr(n):

    is_prime = [True]*(n+2)

    is_prime[0] = False

    is_prime[1] = False

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

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

            if is_prime[i] == False:

                continue

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

                is_prime[k] = False

                

    return is_prime

    

L = pr(10**5*2+2000)



li = []

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

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

        li.append(i)



for i in range(q):

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

    l_ = bisect.bisect_left(li,l)

    r_ = bisect.bisect_right(li,r)

    print((r_ - l_))

q = int (eval(input()))



def pr(n):

    L = [1]*(n+1)

    L[0] = 0

    L[1] = 0

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

        for j in range(2*i,n,i):

            L[j] = 0

    return L

    

a = pr(100001)



li = [0]*(100001)

for i in range(1,100000,2):

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

        li[i] = 1

        

from itertools import accumulate

b = list(accumulate(li))



for i in range(q):

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

    print((b[r]-b[l-1]))
import bisect

q = int(eval(input()))


def pr(n):
    is_prime = [True] * (n + 2)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int((n + 1) ** 0.5) + 1):
        for j in range(2, i + 1):
            if is_prime[i] == False:
                continue
            for k in range(2 * i, n + 1, i):
                is_prime[k] = False
    return is_prime


L = pr(10**5 * 2 + 2000)
li = []
for i in range(2, 10**5 + 1):
    if L[i] and L[(i + 1) // 2]:
        li.append(i)
for i in range(q):
    l, r = list(map(int, input().split()))
    l_ = bisect.bisect_left(li, l)
    r_ = bisect.bisect_right(li, r)
    print((r_ - l_))

q = int(eval(input()))


def pr(n):
    L = [1] * (n + 1)
    L[0] = 0
    L[1] = 0
    for i in range(2, n + 1):
        for j in range(2 * i, n, i):
            L[j] = 0
    return L


a = pr(100001)
li = [0] * (100001)
for i in range(1, 100000, 2):
    if a[i] and a[(i + 1) // 2]:
        li[i] = 1
from itertools import accumulate

b = list(accumulate(li))
for i in range(q):
    l, r = list(map(int, input().split()))
    print((b[r] - b[l - 1]))

-import bisect - -    is_prime = [True] * (n + 2) -    is_prime[0] = False -    is_prime[1] = False -    for i in range(2, int((n + 1) ** 0.5) + 1): -        for j in range(2, i + 1): -            if is_prime[i] == False: -                continue -            for k in range(2 * i, n + 1, i): -                is_prime[k] = False -    return is_prime +    L = [1] * (n + 1) +    L[0] = 0 +    L[1] = 0 +    for i in range(2, n + 1): +        for j in range(2 * i, n, i): +            L[j] = 0 +    return L -L = pr(10**5 * 2 + 2000) -li = [] -for i in range(2, 10**5 + 1): -    if L[i] and L[(i + 1) // 2]: -        li.append(i) +a = pr(100001) +li = [0] * (100001) +for i in range(1, 100000, 2): +    if a[i] and a[(i + 1) // 2]: +        li[i] = 1 +from itertools import accumulate + +b = list(accumulate(li)) -    l_ = bisect.bisect_left(li, l) -    r_ = bisect.bisect_right(li, r) -    print((r_ - l_)) +    print((b[r] - b[l - 1]))