# O(nloglogn) https://mathtrain.jp/eratosthenes

def sieve(n):

	s = [True] * n

	s[0] = s[1] = False

	for x in range(2, int(n ** 0.5) + 1):

		if s[x]:

			for i in range(2 * x, n, x):

				s[i] = False

	return s



is_prime = sieve(10 ** 5 + 1)

is_like2017 = [False] * (10 ** 5 + 1)

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

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

		is_like2017[i] = True



# 区間内のlike2017を求める

# 累積和を用いて高速化

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

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

	if is_like2017[i]:

		c[i] = c[i - 1] + 1

	else:

		c[i] = c[i-1]



# 出力

q = int(eval(input()))



for i in range(q):

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

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

q = int(eval(input()))



def is_prime(n):

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

        if n % i == 0:

            return False

    return n != 1



def main(n):

    return 1 if is_prime(n) and is_prime((n + 1) // 2) else 0



MAX = 10 ** 5 + 100

A = [main(i) for i in range(MAX)]

# 累積和

acc = [0] * (MAX + 1)

for i in range(MAX):

    acc[i + 1] = acc[i] + A[i]



for i in range(q):

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

    # [l, r] なので r + 1 にする

    print((acc[r + 1] - acc[l]))
# O(nloglogn) https://mathtrain.jp/eratosthenes
def sieve(n):
    s = [True] * n
    s[0] = s[1] = False
    for x in range(2, int(n**0.5) + 1):
        if s[x]:
            for i in range(2 * x, n, x):
                s[i] = False
    return s


is_prime = sieve(10**5 + 1)
is_like2017 = [False] * (10**5 + 1)
for i in range(10**5 + 1):
    if is_prime[i] and is_prime[(i + 1) // 2]:
        is_like2017[i] = True
# 区間内のlike2017を求める
# 累積和を用いて高速化
c = [0] * (10**5 + 1)
for i in range(1, 10**5 + 1):
    if is_like2017[i]:
        c[i] = c[i - 1] + 1
    else:
        c[i] = c[i - 1]
# 出力
q = int(eval(input()))
for i in range(q):
    l, r = list(map(int, input().split()))
    print((c[r] - c[l - 1]))

q = int(eval(input()))


def is_prime(n):
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return n != 1


def main(n):
    return 1 if is_prime(n) and is_prime((n + 1) // 2) else 0


MAX = 10**5 + 100
A = [main(i) for i in range(MAX)]
# 累積和
acc = [0] * (MAX + 1)
for i in range(MAX):
    acc[i + 1] = acc[i] + A[i]
for i in range(q):
    l, r = list(map(int, input().split()))
    # [l, r] なので r + 1 にする
    print((acc[r + 1] - acc[l]))

-# O(nloglogn) https://mathtrain.jp/eratosthenes -def sieve(n): -    s = [True] * n -    s[0] = s[1] = False -    for x in range(2, int(n**0.5) + 1): -        if s[x]: -            for i in range(2 * x, n, x): -                s[i] = False -    return s +q = int(eval(input())) -is_prime = sieve(10**5 + 1) -is_like2017 = [False] * (10**5 + 1) -for i in range(10**5 + 1): -    if is_prime[i] and is_prime[(i + 1) // 2]: -        is_like2017[i] = True -# 区間内のlike2017を求める -# 累積和を用いて高速化 -c = [0] * (10**5 + 1) -for i in range(1, 10**5 + 1): -    if is_like2017[i]: -        c[i] = c[i - 1] + 1 -    else: -        c[i] = c[i - 1] -# 出力 -q = int(eval(input())) +def is_prime(n): +    for i in range(2, int(n**0.5) + 1): +        if n % i == 0: +            return False +    return n != 1 + + +def main(n): +    return 1 if is_prime(n) and is_prime((n + 1) // 2) else 0 + + +MAX = 10**5 + 100 +A = [main(i) for i in range(MAX)] +# 累積和 +acc = [0] * (MAX + 1) +for i in range(MAX): +    acc[i + 1] = acc[i] + A[i] -    print((c[r] - c[l - 1])) +    # [l, r] なので r + 1 にする +    print((acc[r + 1] - acc[l]))