import math

def sieve(n):

    if n < 2:

        is_prime = [False for _ in range(n+1)]

        return is_prime

 

    is_prime = [True for _ in range(n+1)]

    is_prime[0] = False

    is_prime[1] = False

 

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

        if is_prime[i]:

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

                is_prime[j] = False

 

    return is_prime



N=10**5

prime=sieve(N)

a=[0 for i in range(N+1)]

for i in range(3,N):

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

	else: a[i]=a[i-1]



Q=int(eval(input()))

for i in range(Q):

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

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



def sieve(n):

    if n < 2:

        is_prime = [False for _ in range(n+1)]

        return is_prime



    is_prime = [True for _ in range(n+1)]

    is_prime[0] = False

    is_prime[1] = False



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

        if is_prime[i]:

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

                is_prime[j] = False



    return is_prime





def main():

    input = sys.stdin.readline

    Q = int(eval(input()))

    is_prime = sieve(10**5)

    A = [0 for _ in range(10**5 + 1)]

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

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

            A[i] = A[i-1] + 1

        else:

            A[i] = A[i-1]



    for _ in range(Q):

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

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





if __name__ == '__main__':

    main()

import math


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


N = 10**5
prime = sieve(N)
a = [0 for i in range(N + 1)]
for i in range(3, N):
    if prime[i] and prime[(i + 1) // 2]:
        a[i] = a[i - 1] + 1
    else:
        a[i] = a[i - 1]
Q = int(eval(input()))
for i in range(Q):
    l, r = list(map(int, input().split()))
    print((a[r] - a[l - 1]))

import sys


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


def main():
    input = sys.stdin.readline
    Q = int(eval(input()))
    is_prime = sieve(10**5)
    A = [0 for _ in range(10**5 + 1)]
    for i in range(3, 10**5 + 1):
        if is_prime[i] and is_prime[(i + 1) // 2]:
            A[i] = A[i - 1] + 1
        else:
            A[i] = A[i - 1]
    for _ in range(Q):
        l, r = list(map(int, input().split()))
        print((A[r] - A[l - 1]))


if __name__ == "__main__":
    main()

-import math +import sys -N = 10**5 -prime = sieve(N) -a = [0 for i in range(N + 1)] -for i in range(3, N): -    if prime[i] and prime[(i + 1) // 2]: -        a[i] = a[i - 1] + 1 -    else: -        a[i] = a[i - 1] -Q = int(eval(input())) -for i in range(Q): -    l, r = list(map(int, input().split())) -    print((a[r] - a[l - 1])) +def main(): +    input = sys.stdin.readline +    Q = int(eval(input())) +    is_prime = sieve(10**5) +    A = [0 for _ in range(10**5 + 1)] +    for i in range(3, 10**5 + 1): +        if is_prime[i] and is_prime[(i + 1) // 2]: +            A[i] = A[i - 1] + 1 +        else: +            A[i] = A[i - 1] +    for _ in range(Q): +        l, r = list(map(int, input().split())) +        print((A[r] - A[l - 1])) + + +if __name__ == "__main__": +    main()