import sys



def I(): return int(sys.stdin.readline().rstrip())

def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり



Q = I()

lr = [LI() for i in range(Q)]



import math



def sieve_of_eratosthenes(n):

    prime_list = []  # n以下の素数のリスト

    A = [1]*(n+1)    # A[i] = iが素数なら1,その他は0

    A[0] = A[1] = 0

    for i in range(2,math.floor(math.sqrt(n))+1):

        if A[i]:

            prime_list.append(i)

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

                A[j] = 0

    for i in range(math.floor(math.sqrt(n)),n+1):

        if A[i] == 1:

            prime_list.append(i)

    return A



prime = sieve_of_eratosthenes(10**5)

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

prime_like_2017[3] = 1



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

    if prime[i] == 0:

        continue

    elif i == 2:

        continue

    elif i == 3:

        prime_like_2017[i] = 1

    elif i % 4 == 3:

        continue

    else:

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

            prime_like_2017[i] = 1



from itertools import accumulate



P = list(accumulate(prime_like_2017))  # P[i] = iまでに2017に似た数が何個あるか



for i in range(Q):

    print((P[lr[i][1]]-P[lr[i][0]-1]))
import sys



sys.setrecursionlimit(10**7)

def I(): return int(sys.stdin.readline().rstrip())

def MI(): return list(map(int,sys.stdin.readline().rstrip().split()))

def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり

def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし

def S(): return sys.stdin.readline().rstrip()

def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり

def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし





import math





def sieve_of_eratosthenes(n):

    prime_list = []  # n以下の素数のリスト

    A = [1]*(n+1)    # A[i] = iが素数なら1,その他は0

    A[0] = A[1] = 0

    for i in range(2,math.floor(math.sqrt(n))+1):

        if A[i]:

            prime_list.append(i)

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

                A[j] = 0

    for i in range(math.floor(math.sqrt(n))+1,n+1):

        if A[i] == 1:

            prime_list.append(i)

    return prime_list,A





prime_list,A = sieve_of_eratosthenes(10**5)



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

for p in prime_list:

    if p == 2:

        continue

    else:

        if A[(p+1)//2] == 1:

            B[p] = 1



from itertools import accumulate



C = list(accumulate(B))



Q = I()

for i in range(Q):

    l,r = MI()

    print((C[r]-C[l-1]))





import sys


def I():
    return int(sys.stdin.readline().rstrip())


def LI():
    return list(map(int, sys.stdin.readline().rstrip().split()))  # 空白あり


Q = I()
lr = [LI() for i in range(Q)]
import math


def sieve_of_eratosthenes(n):
    prime_list = []  # n以下の素数のリスト
    A = [1] * (n + 1)  # A[i] = iが素数なら1,その他は0
    A[0] = A[1] = 0
    for i in range(2, math.floor(math.sqrt(n)) + 1):
        if A[i]:
            prime_list.append(i)
            for j in range(i**2, n + 1, i):
                A[j] = 0
    for i in range(math.floor(math.sqrt(n)), n + 1):
        if A[i] == 1:
            prime_list.append(i)
    return A


prime = sieve_of_eratosthenes(10**5)
prime_like_2017 = [0] * (10**5 + 1)
prime_like_2017[3] = 1
for i in range(3, 10**5 + 1):
    if prime[i] == 0:
        continue
    elif i == 2:
        continue
    elif i == 3:
        prime_like_2017[i] = 1
    elif i % 4 == 3:
        continue
    else:
        if prime[(i + 1) // 2] == 1:
            prime_like_2017[i] = 1
from itertools import accumulate

P = list(accumulate(prime_like_2017))  # P[i] = iまでに2017に似た数が何個あるか
for i in range(Q):
    print((P[lr[i][1]] - P[lr[i][0] - 1]))

import sys

sys.setrecursionlimit(10**7)


def I():
    return int(sys.stdin.readline().rstrip())


def MI():
    return list(map(int, sys.stdin.readline().rstrip().split()))


def LI():
    return list(map(int, sys.stdin.readline().rstrip().split()))  # 空白あり


def LI2():
    return list(map(int, sys.stdin.readline().rstrip()))  # 空白なし


def S():
    return sys.stdin.readline().rstrip()


def LS():
    return list(sys.stdin.readline().rstrip().split())  # 空白あり


def LS2():
    return list(sys.stdin.readline().rstrip())  # 空白なし


import math


def sieve_of_eratosthenes(n):
    prime_list = []  # n以下の素数のリスト
    A = [1] * (n + 1)  # A[i] = iが素数なら1,その他は0
    A[0] = A[1] = 0
    for i in range(2, math.floor(math.sqrt(n)) + 1):
        if A[i]:
            prime_list.append(i)
            for j in range(i**2, n + 1, i):
                A[j] = 0
    for i in range(math.floor(math.sqrt(n)) + 1, n + 1):
        if A[i] == 1:
            prime_list.append(i)
    return prime_list, A


prime_list, A = sieve_of_eratosthenes(10**5)
B = [0] * (10**5 + 1)
for p in prime_list:
    if p == 2:
        continue
    else:
        if A[(p + 1) // 2] == 1:
            B[p] = 1
from itertools import accumulate

C = list(accumulate(B))
Q = I()
for i in range(Q):
    l, r = MI()
    print((C[r] - C[l - 1]))

+ +sys.setrecursionlimit(10**7) +def MI(): +    return list(map(int, sys.stdin.readline().rstrip().split())) + + -Q = I() -lr = [LI() for i in range(Q)] +def LI2(): +    return list(map(int, sys.stdin.readline().rstrip()))  # 空白なし + + +def S(): +    return sys.stdin.readline().rstrip() + + +def LS(): +    return list(sys.stdin.readline().rstrip().split())  # 空白あり + + +def LS2(): +    return list(sys.stdin.readline().rstrip())  # 空白なし + + -    for i in range(math.floor(math.sqrt(n)), n + 1): +    for i in range(math.floor(math.sqrt(n)) + 1, n + 1): -    return A +    return prime_list, A -prime = sieve_of_eratosthenes(10**5) -prime_like_2017 = [0] * (10**5 + 1) -prime_like_2017[3] = 1 -for i in range(3, 10**5 + 1): -    if prime[i] == 0: -        continue -    elif i == 2: -        continue -    elif i == 3: -        prime_like_2017[i] = 1 -    elif i % 4 == 3: +prime_list, A = sieve_of_eratosthenes(10**5) +B = [0] * (10**5 + 1) +for p in prime_list: +    if p == 2: -        if prime[(i + 1) // 2] == 1: -            prime_like_2017[i] = 1 +        if A[(p + 1) // 2] == 1: +            B[p] = 1 -P = list(accumulate(prime_like_2017))  # P[i] = iまでに2017に似た数が何個あるか +C = list(accumulate(B)) +Q = I() -    print((P[lr[i][1]] - P[lr[i][0] - 1])) +    l, r = MI() +    print((C[r] - C[l - 1]))