#!/usr/bin/env python3

import sys

from bisect import bisect_right





def cal_prime(N):

    prime_list = set()

    ans_list = set()

    for i in range(2, N):

        flag = True

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

            if i % j == 0:

                flag = False

                break

        if flag:

            prime_list.add(i)

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

                ans_list.add(i)

    return sorted(ans_list)





def main():

    Q = int(eval(input()))

    ans_list = cal_prime(10**5)



    for i in range(Q):

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

        print((bisect_right(ans_list, r)-bisect_right(ans_list, l-1)))





if __name__ == '__main__':

    main()

def cal_primes(N):

    candidate = [*list(range(2, N+1))]

    primes = []



    while candidate[0]**2 <= N:

        primes.append(candidate[0])

        candidate = [*[x for x in candidate if x % candidate[0] != 0]]

    

    primes.extend(candidate)

    return primes



Q = int(eval(input()))

primes = set(cal_primes(10**5))

# print(primes)

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

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

    #print(i)

    if i%2==1 and i in primes and (i+1)//2 in primes:

        res[i] = res[i-1] + 1

    else:

        res[i] = res[i-1]

#print(res)

for i in range(Q):

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

    print((res[r]-res[l-1]))
#!/usr/bin/env python3
import sys
from bisect import bisect_right


def cal_prime(N):
    prime_list = set()
    ans_list = set()
    for i in range(2, N):
        flag = True
        for j in range(2, int(i**0.5) + 1):
            if i % j == 0:
                flag = False
                break
        if flag:
            prime_list.add(i)
            if i > 2 and (i + 2) // 2 in prime_list:
                ans_list.add(i)
    return sorted(ans_list)


def main():
    Q = int(eval(input()))
    ans_list = cal_prime(10**5)
    for i in range(Q):
        l, r = list(map(int, input().split()))
        print((bisect_right(ans_list, r) - bisect_right(ans_list, l - 1)))


if __name__ == "__main__":
    main()

def cal_primes(N):
    candidate = [*list(range(2, N + 1))]
    primes = []
    while candidate[0] ** 2 <= N:
        primes.append(candidate[0])
        candidate = [*[x for x in candidate if x % candidate[0] != 0]]
    primes.extend(candidate)
    return primes


Q = int(eval(input()))
primes = set(cal_primes(10**5))
# print(primes)
res = [0] * (10**5 + 1)
for i in range(1, 10**5 + 1):
    # print(i)
    if i % 2 == 1 and i in primes and (i + 1) // 2 in primes:
        res[i] = res[i - 1] + 1
    else:
        res[i] = res[i - 1]
# print(res)
for i in range(Q):
    l, r = list(map(int, input().split()))
    print((res[r] - res[l - 1]))

-#!/usr/bin/env python3 -import sys -from bisect import bisect_right +def cal_primes(N): +    candidate = [*list(range(2, N + 1))] +    primes = [] +    while candidate[0] ** 2 <= N: +        primes.append(candidate[0]) +        candidate = [*[x for x in candidate if x % candidate[0] != 0]] +    primes.extend(candidate) +    return primes -def cal_prime(N): -    prime_list = set() -    ans_list = set() -    for i in range(2, N): -        flag = True -        for j in range(2, int(i**0.5) + 1): -            if i % j == 0: -                flag = False -                break -        if flag: -            prime_list.add(i) -            if i > 2 and (i + 2) // 2 in prime_list: -                ans_list.add(i) -    return sorted(ans_list) - - -def main(): -    Q = int(eval(input())) -    ans_list = cal_prime(10**5) -    for i in range(Q): -        l, r = list(map(int, input().split())) -        print((bisect_right(ans_list, r) - bisect_right(ans_list, l - 1))) - - -if __name__ == "__main__": -    main() +Q = int(eval(input())) +primes = set(cal_primes(10**5)) +# print(primes) +res = [0] * (10**5 + 1) +for i in range(1, 10**5 + 1): +    # print(i) +    if i % 2 == 1 and i in primes and (i + 1) // 2 in primes: +        res[i] = res[i - 1] + 1 +    else: +        res[i] = res[i - 1] +# print(res) +for i in range(Q): +    l, r = list(map(int, input().split())) +    print((res[r] - res[l - 1]))