import sys

stdin = sys.stdin



sys.setrecursionlimit(10**5) 

 

def li(): return list(map(int, stdin.readline().split()))

def li_(): return [int(x)-1 for x in stdin.readline().split()]

def lf(): return list(map(float, stdin.readline().split()))

def ls(): return stdin.readline().split()

def ns(): return stdin.readline().rstrip()

def lc(): return list(ns())

def ni(): return int(stdin.readline())

def nf(): return float(stdin.readline())



from math import sqrt

from bisect import bisect_left, bisect_right



# 半開区間[lower, upper)の素数のリストを作成する

def make_prime_list(lower:int, upper:int) -> list:

    

    # 素数リストの初期化    

    isPrime = [True]*upper

    primeList = []

    

    # 区間内の数字が0,1のみならここで終了

    if upper <= 2:

        return primeList

    

    # 区間内の数字に2以上のものがあるとき

    isPrime[0] = False

    isPrime[1] = False

    

    # エラトステネスの篩の処理

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

        if isPrime[n]:

            res=2*n

            while res<upper:

                isPrime[res] = False

                res += n

    

    # 区間内の素数を抽出

    for n in range(lower,upper):

        if isPrime[n]:

            primeList.append(n)

        

    return primeList



q = ni()

query = [tuple(li()) for _ in range(q)]



prime_list = set(make_prime_list(2,10**5+1))



like2017 = []



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

    if (i in prime_list) and ((i+1)//2 in prime_list):

        like2017.append(i)

        

for l,r in query:

    print((bisect_right(like2017, r) - bisect_left(like2017, l)))

    
import sys

stdin = sys.stdin

 

sys.setrecursionlimit(10**5) 

 

def li(): return list(map(int, stdin.readline().split()))

def li_(): return [int(x)-1 for x in stdin.readline().split()]

def lf(): return list(map(float, stdin.readline().split()))

def ls(): return stdin.readline().split()

def ns(): return stdin.readline().rstrip()

def lc(): return list(ns())

def ni(): return int(stdin.readline())

def nf(): return float(stdin.readline())



def make_prime_list(lower: int, upper: int) -> list:

    if upper <= 2:

        return []

    

    prime_list = []

    

    is_prime = [True]*upper

    is_prime[0] = False

    is_prime[1] = False

    

    n = 2

    while n**2 < upper:

        if is_prime[n]:

            res = 2*n

            while res < upper:

                is_prime[res] = False

                res += n

                

        n += 1

        

    for i in range(lower, upper):

        if is_prime[i]:

            prime_list.append(i)

            

    return prime_list



def is_like2017(num: int, prime_set) -> int:

    if num in prime_set and (num+1)//2 in prime_set:

        return 1

    else:

        return 0



from itertools import accumulate



MAX_N = 10**5+1



prime_list = make_prime_list(1,MAX_N)

prime_set = set(prime_list)



prime_cnt = [0]*MAX_N



for i in range(1,MAX_N,2):

    prime_cnt[i] += is_like2017(i, prime_set)

        

prime_cum = list(accumulate(prime_cnt))





q = ni()

for _ in range(q):

    l,r = li()

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

stdin = sys.stdin
sys.setrecursionlimit(10**5)


def li():
    return list(map(int, stdin.readline().split()))


def li_():
    return [int(x) - 1 for x in stdin.readline().split()]


def lf():
    return list(map(float, stdin.readline().split()))


def ls():
    return stdin.readline().split()


def ns():
    return stdin.readline().rstrip()


def lc():
    return list(ns())


def ni():
    return int(stdin.readline())


def nf():
    return float(stdin.readline())


from math import sqrt
from bisect import bisect_left, bisect_right

# 半開区間[lower, upper)の素数のリストを作成する
def make_prime_list(lower: int, upper: int) -> list:
    # 素数リストの初期化
    isPrime = [True] * upper
    primeList = []
    # 区間内の数字が0,1のみならここで終了
    if upper <= 2:
        return primeList
    # 区間内の数字に2以上のものがあるとき
    isPrime[0] = False
    isPrime[1] = False
    # エラトステネスの篩の処理
    for n in range(2, int(sqrt(upper)) + 2):
        if isPrime[n]:
            res = 2 * n
            while res < upper:
                isPrime[res] = False
                res += n
    # 区間内の素数を抽出
    for n in range(lower, upper):
        if isPrime[n]:
            primeList.append(n)
    return primeList


q = ni()
query = [tuple(li()) for _ in range(q)]
prime_list = set(make_prime_list(2, 10**5 + 1))
like2017 = []
for i in range(3, 10**5, 2):
    if (i in prime_list) and ((i + 1) // 2 in prime_list):
        like2017.append(i)
for l, r in query:
    print((bisect_right(like2017, r) - bisect_left(like2017, l)))

import sys

stdin = sys.stdin
sys.setrecursionlimit(10**5)


def li():
    return list(map(int, stdin.readline().split()))


def li_():
    return [int(x) - 1 for x in stdin.readline().split()]


def lf():
    return list(map(float, stdin.readline().split()))


def ls():
    return stdin.readline().split()


def ns():
    return stdin.readline().rstrip()


def lc():
    return list(ns())


def ni():
    return int(stdin.readline())


def nf():
    return float(stdin.readline())


def make_prime_list(lower: int, upper: int) -> list:
    if upper <= 2:
        return []
    prime_list = []
    is_prime = [True] * upper
    is_prime[0] = False
    is_prime[1] = False
    n = 2
    while n**2 < upper:
        if is_prime[n]:
            res = 2 * n
            while res < upper:
                is_prime[res] = False
                res += n
        n += 1
    for i in range(lower, upper):
        if is_prime[i]:
            prime_list.append(i)
    return prime_list


def is_like2017(num: int, prime_set) -> int:
    if num in prime_set and (num + 1) // 2 in prime_set:
        return 1
    else:
        return 0


from itertools import accumulate

MAX_N = 10**5 + 1
prime_list = make_prime_list(1, MAX_N)
prime_set = set(prime_list)
prime_cnt = [0] * MAX_N
for i in range(1, MAX_N, 2):
    prime_cnt[i] += is_like2017(i, prime_set)
prime_cum = list(accumulate(prime_cnt))
q = ni()
for _ in range(q):
    l, r = li()
    print((prime_cum[r] - prime_cum[l - 1]))

-from math import sqrt -from bisect import bisect_left, bisect_right - -# 半開区間[lower, upper)の素数のリストを作成する -    # 素数リストの初期化 -    isPrime = [True] * upper -    primeList = [] -    # 区間内の数字が0,1のみならここで終了 -        return primeList -    # 区間内の数字に2以上のものがあるとき -    isPrime[0] = False -    isPrime[1] = False -    # エラトステネスの篩の処理 -    for n in range(2, int(sqrt(upper)) + 2): -        if isPrime[n]: +        return [] +    prime_list = [] +    is_prime = [True] * upper +    is_prime[0] = False +    is_prime[1] = False +    n = 2 +    while n**2 < upper: +        if is_prime[n]: -                isPrime[res] = False +                is_prime[res] = False -    # 区間内の素数を抽出 -    for n in range(lower, upper): -        if isPrime[n]: -            primeList.append(n) -    return primeList +        n += 1 +    for i in range(lower, upper): +        if is_prime[i]: +            prime_list.append(i) +    return prime_list +def is_like2017(num: int, prime_set) -> int: +    if num in prime_set and (num + 1) // 2 in prime_set: +        return 1 +    else: +        return 0 + + +from itertools import accumulate + +MAX_N = 10**5 + 1 +prime_list = make_prime_list(1, MAX_N) +prime_set = set(prime_list) +prime_cnt = [0] * MAX_N +for i in range(1, MAX_N, 2): +    prime_cnt[i] += is_like2017(i, prime_set) +prime_cum = list(accumulate(prime_cnt)) -query = [tuple(li()) for _ in range(q)] -prime_list = set(make_prime_list(2, 10**5 + 1)) -like2017 = [] -for i in range(3, 10**5, 2): -    if (i in prime_list) and ((i + 1) // 2 in prime_list): -        like2017.append(i) -for l, r in query: -    print((bisect_right(like2017, r) - bisect_left(like2017, l))) +for _ in range(q): +    l, r = li() +    print((prime_cum[r] - prime_cum[l - 1]))