import math

import itertools

Q = int(eval(input()))

query = [list(map(int,input().split())) for i in range(Q)]



def is_prime(n):

        if n <= 0:return None

        if n == 1:return False

        sup = math.floor(pow(n,1/2))

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

                if n % i == 0:

                        return False

        return True



prime = [0]*(10**5+1)

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

        if is_prime(i):

                prime[i] = 1



for p,bit in enumerate(prime[::-1]):

        p = len(prime)-1 - p

        if bit and p % 2 == 1 and prime[(p+1)//2]:

                pass

        else:

                prime[p] = 0



acc = list(itertools.accumulate(prime))

for l,r in query:

        print((acc[r]-acc[l-1]))
import sys,math,itertools

input = sys.stdin.readline

Q = int(eval(input()))

query = [list(map(int,input().split())) for i in range(Q)]



def is_prime(n):

        if n <= 0:return None

        if n == 1:return False

        sup = math.floor(pow(n,1/2))

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

                if n % i == 0:

                        return False

        return True



prime = [0]*(10**5+1)

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

        if is_prime(i):

                prime[i] = 1



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

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

                pass

        else:

                prime[i] = 0



acc = list(itertools.accumulate(prime))

for l,r in query:

        print((acc[r]-acc[l-1]))
import math
import itertools

Q = int(eval(input()))
query = [list(map(int, input().split())) for i in range(Q)]


def is_prime(n):
    if n <= 0:
        return None
    if n == 1:
        return False
    sup = math.floor(pow(n, 1 / 2))
    for i in range(2, sup + 1):
        if n % i == 0:
            return False
    return True


prime = [0] * (10**5 + 1)
for i in range(1, 10**5 + 1):
    if is_prime(i):
        prime[i] = 1
for p, bit in enumerate(prime[::-1]):
    p = len(prime) - 1 - p
    if bit and p % 2 == 1 and prime[(p + 1) // 2]:
        pass
    else:
        prime[p] = 0
acc = list(itertools.accumulate(prime))
for l, r in query:
    print((acc[r] - acc[l - 1]))

import sys, math, itertools

input = sys.stdin.readline
Q = int(eval(input()))
query = [list(map(int, input().split())) for i in range(Q)]


def is_prime(n):
    if n <= 0:
        return None
    if n == 1:
        return False
    sup = math.floor(pow(n, 1 / 2))
    for i in range(2, sup + 1):
        if n % i == 0:
            return False
    return True


prime = [0] * (10**5 + 1)
for i in range(1, 10**5 + 1):
    if is_prime(i):
        prime[i] = 1
for i in range(10**5, -1, -1):
    if prime[i] and i % 2 == 1 and prime[(i + 1) // 2]:
        pass
    else:
        prime[i] = 0
acc = list(itertools.accumulate(prime))
for l, r in query:
    print((acc[r] - acc[l - 1]))

-import math -import itertools +import sys, math, itertools +input = sys.stdin.readline -for p, bit in enumerate(prime[::-1]): -    p = len(prime) - 1 - p -    if bit and p % 2 == 1 and prime[(p + 1) // 2]: +for i in range(10**5, -1, -1): +    if prime[i] and i % 2 == 1 and prime[(i + 1) // 2]: -        prime[p] = 0 +        prime[i] = 0