import math

def Sieve_of_Eratosthenes(N):

	prime={}

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

		prime[i]=1



	for i in range(2,math.ceil(N**0.5)+1):

		tmp=set()

		for j in prime:

			if j>i and j%i==0: tmp.add(j)

		for j in tmp:

			prime.pop(j)



	return prime



N=10**5

prime=Sieve_of_Eratosthenes(N)

a=[0]*(N)

for i in range(2,N):

	if i in prime and (i+1)//2 in prime: 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

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]*(N)

for i in range(2,N):

	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


def Sieve_of_Eratosthenes(N):
    prime = {}
    for i in range(2, N + 1):
        prime[i] = 1
    for i in range(2, math.ceil(N**0.5) + 1):
        tmp = set()
        for j in prime:
            if j > i and j % i == 0:
                tmp.add(j)
        for j in tmp:
            prime.pop(j)
    return prime


N = 10**5
prime = Sieve_of_Eratosthenes(N)
a = [0] * (N)
for i in range(2, N):
    if i in prime and (i + 1) // 2 in prime:
        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


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] * (N)
for i in range(2, N):
    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 Sieve_of_Eratosthenes(N): -    prime = {} -    for i in range(2, N + 1): -        prime[i] = 1 -    for i in range(2, math.ceil(N**0.5) + 1): -        tmp = set() -        for j in prime: -            if j > i and j % i == 0: -                tmp.add(j) -        for j in tmp: -            prime.pop(j) -    return prime +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 -prime = Sieve_of_Eratosthenes(N) +prime = sieve(N) -    if i in prime and (i + 1) // 2 in prime: +    if prime[i] and prime[(i + 1) // 2]: