import math

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():

	N=10**5

	prime=sieve(N)

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

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

		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]))





if __name__ == '__main__':

    main()
import math

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



input = sys.stdin.readline

N=10**5

prime=sieve(N)

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

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

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

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



for i in range(int(eval(input()))):

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

	print((a[r]-a[l-1]))
import math
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():
    N = 10**5
    prime = sieve(N)
    a = [0 for i in range(N + 1)]
    for i in range(3, N + 1):
        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]))


if __name__ == "__main__":
    main()

import math
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


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

-def main(): -    N = 10**5 -    prime = sieve(N) -    a = [0 for i in range(N + 1)] -    for i in range(3, N + 1): -        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])) - - -if __name__ == "__main__": -    main() +input = sys.stdin.readline +N = 10**5 +prime = sieve(N) +a = [0 for i in range(N + 1)] +for i in range(3, N + 1): +    if prime[i] and prime[(i + 1) // 2]: +        a[i] = a[i - 1] + 1 +    else: +        a[i] = a[i - 1] +for i in range(int(eval(input()))): +    l, r = list(map(int, input().split())) +    print((a[r] - a[l - 1]))