

import math

def eratosthenes(n):

    prime = []

    limit = math.sqrt(n)

    data = [i+1 for i in range(1,n)]

    while True:

        p = data[0]

        if limit <= p:

            return prime + data

        prime.append(p)

        data = [e for e in data if e%p != 0]



_primes = eratosthenes(10**5)

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



for p in _primes:

    primes[p] = True



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

for p in _primes:

    if primes[(p+1)//2]:

        like_2017[p] = True



c = 0

C = [0]

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

    if like_2017[i]:

        c += 1

    C.append(c)



q = int(eval(input()))

for _ in range(q):

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

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



def is_prime(x):

    if x==1:

        return False

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

        if x%i==0:

            return False

    return True



lst = [] 

for x in range(1,10**5,2):

    if is_prime(x):

        lst.append(x)



lst_dash = []

for x in lst:

    if is_prime((x+1)//2):

        lst_dash.append(x)



q = int(input())

ans = []

for i in range(q):

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

    left = bisect.bisect_left(lst_dash, l)

    right = bisect.bisect_right(lst_dash, r)

    ans.append(right - left)



print(*ans, sep='\n')

import math


def eratosthenes(n):
    prime = []
    limit = math.sqrt(n)
    data = [i + 1 for i in range(1, n)]
    while True:
        p = data[0]
        if limit <= p:
            return prime + data
        prime.append(p)
        data = [e for e in data if e % p != 0]


_primes = eratosthenes(10**5)
primes = [False] * (10**5 + 1)
for p in _primes:
    primes[p] = True
like_2017 = [False] * (10**5 + 1)
for p in _primes:
    if primes[(p + 1) // 2]:
        like_2017[p] = True
c = 0
C = [0]
for i in range(1, 10**5 + 1):
    if like_2017[i]:
        c += 1
    C.append(c)
q = int(eval(input()))
for _ in range(q):
    l, r = list(map(int, input().split()))
    print((C[r] - C[l - 1]))

import bisect


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


lst = []
for x in range(1, 10**5, 2):
    if is_prime(x):
        lst.append(x)
lst_dash = []
for x in lst:
    if is_prime((x + 1) // 2):
        lst_dash.append(x)
q = int(input())
ans = []
for i in range(q):
    l, r = map(int, input().split())
    left = bisect.bisect_left(lst_dash, l)
    right = bisect.bisect_right(lst_dash, r)
    ans.append(right - left)
print(*ans, sep="\n")

-import math +import bisect -def eratosthenes(n): -    prime = [] -    limit = math.sqrt(n) -    data = [i + 1 for i in range(1, n)] -    while True: -        p = data[0] -        if limit <= p: -            return prime + data -        prime.append(p) -        data = [e for e in data if e % p != 0] +def is_prime(x): +    if x == 1: +        return False +    for i in range(2, int(x**0.5) + 1): +        if x % i == 0: +            return False +    return True -_primes = eratosthenes(10**5) -primes = [False] * (10**5 + 1) -for p in _primes: -    primes[p] = True -like_2017 = [False] * (10**5 + 1) -for p in _primes: -    if primes[(p + 1) // 2]: -        like_2017[p] = True -c = 0 -C = [0] -for i in range(1, 10**5 + 1): -    if like_2017[i]: -        c += 1 -    C.append(c) -q = int(eval(input())) -for _ in range(q): -    l, r = list(map(int, input().split())) -    print((C[r] - C[l - 1])) +lst = [] +for x in range(1, 10**5, 2): +    if is_prime(x): +        lst.append(x) +lst_dash = [] +for x in lst: +    if is_prime((x + 1) // 2): +        lst_dash.append(x) +q = int(input()) +ans = [] +for i in range(q): +    l, r = map(int, input().split()) +    left = bisect.bisect_left(lst_dash, l) +    right = bisect.bisect_right(lst_dash, r) +    ans.append(right - left) +print(*ans, sep="\n")