# D

from math import sqrt

def sieve(n:int)->list:

    """エラトステネスの篩"""

    if n<2:  return [False]*(n+1)

    is_prime = [True]*(n+1)

    is_prime[0] = False

    is_prime[1] = False

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

        if is_prime[i]:

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

                is_prime[j] = False

    

    return is_prime





N = int(1e5)

is_prime = sieve(N)

# like-2017

cnt = [0]*(N+1)

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

    cnt[i] += cnt[i-1]

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

        cnt[i] += 1

    

Q = int(eval(input()))

for _ in range(Q):

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

    print((cnt[r]-cnt[l-1]))
# D

from math import sqrt

def sieve(n:int)->list:

    """エラトステネスの篩"""

    if n<2:  return [False]*(n+1)

    is_prime = [True]*(n+1)

    is_prime[0] = False

    is_prime[1] = False

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

        if is_prime[i]:

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

                is_prime[j] = False

    

    return is_prime





N = int(1e5)

is_prime = sieve(N)

# like-2017

cnt = [0]*(N+1)

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

    cnt[i] += cnt[i-1]

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

        cnt[i] += 1

    

import sys

Q = sys.stdin.readline()

for l,r in (list(map(int, line.split())) for line in sys.stdin.readlines()):

    print((cnt[r] - cnt[l-1]))
# D
from math import sqrt


def sieve(n: int) -> list:
    """エラトステネスの篩"""
    if n < 2:
        return [False] * (n + 1)
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(sqrt(n)) + 1):
        if is_prime[i]:
            for j in range(i * 2, n + 1, i):
                is_prime[j] = False
    return is_prime


N = int(1e5)
is_prime = sieve(N)
# like-2017
cnt = [0] * (N + 1)
for i in range(1, N + 1):
    cnt[i] += cnt[i - 1]
    if is_prime[i] and is_prime[(i + 1) // 2]:
        cnt[i] += 1
Q = int(eval(input()))
for _ in range(Q):
    l, r = list(map(int, input().split()))
    print((cnt[r] - cnt[l - 1]))

# D
from math import sqrt


def sieve(n: int) -> list:
    """エラトステネスの篩"""
    if n < 2:
        return [False] * (n + 1)
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(sqrt(n)) + 1):
        if is_prime[i]:
            for j in range(i * 2, n + 1, i):
                is_prime[j] = False
    return is_prime


N = int(1e5)
is_prime = sieve(N)
# like-2017
cnt = [0] * (N + 1)
for i in range(1, N + 1):
    cnt[i] += cnt[i - 1]
    if is_prime[i] and is_prime[(i + 1) // 2]:
        cnt[i] += 1
import sys

Q = sys.stdin.readline()
for l, r in (list(map(int, line.split())) for line in sys.stdin.readlines()):
    print((cnt[r] - cnt[l - 1]))

-Q = int(eval(input())) -for _ in range(Q): -    l, r = list(map(int, input().split())) +import sys + +Q = sys.stdin.readline() +for l, r in (list(map(int, line.split())) for line in sys.stdin.readlines()):