import sys

input = sys.stdin.readline



from itertools import accumulate



import math



def is_prime(n):

    if n <= 0: return None

    if n == 1: return False

    sup = math.floor(pow(n,1/2))

    for i in range(2,sup+1):

        if n % i == 0:

            return False

    return True



Q = int(eval(input()))

query = [list(map(int,input().split())) for _ in range(Q)]



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



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

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

        bit[i] = 1



bit = list(accumulate(bit))



for l, r in query:

    res = bit[r] - bit[l-1]

    print(res)
def sieve(N):

    global table,prime

    table = [True]*(N+1)

    table[0] = False

    table[1] = False

    prime = []

    for i in range(2,N+1):

        if table[i]:

            prime.append(i)

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

                table[j] = False



def accmulate(array):

    global cs

    cs = [0]*(len(array)+1)

    for i in range(len(array)):

        cs[i+1] = cs[i] + array[i]



def query(l,r):

    return cs[r+1] - cs[l]



Q = int(input())



sieve(10**5)



def judge(x):

    return x % 2 and table[x] and table[(x+1)//2]



flag = [judge(x) for x in range(0,10**5+1)]



accmulate(flag)



ans_list = []



for _ in range(Q):

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

    ans = query(l,r+1)

    ans_list.append(ans)



print(*ans_list, sep="\n")

import sys

input = sys.stdin.readline
from itertools import accumulate
import math


def is_prime(n):
    if n <= 0:
        return None
    if n == 1:
        return False
    sup = math.floor(pow(n, 1 / 2))
    for i in range(2, sup + 1):
        if n % i == 0:
            return False
    return True


Q = int(eval(input()))
query = [list(map(int, input().split())) for _ in range(Q)]
bit = [0] * (10**5 + 1)
for i in range(10**5 + 1):
    if i % 2 == 1 and is_prime(i) and is_prime((i + 1) // 2):
        bit[i] = 1
bit = list(accumulate(bit))
for l, r in query:
    res = bit[r] - bit[l - 1]
    print(res)

def sieve(N):
    global table, prime
    table = [True] * (N + 1)
    table[0] = False
    table[1] = False
    prime = []
    for i in range(2, N + 1):
        if table[i]:
            prime.append(i)
            for j in range(2 * i, N + 1, i):
                table[j] = False


def accmulate(array):
    global cs
    cs = [0] * (len(array) + 1)
    for i in range(len(array)):
        cs[i + 1] = cs[i] + array[i]


def query(l, r):
    return cs[r + 1] - cs[l]


Q = int(input())
sieve(10**5)


def judge(x):
    return x % 2 and table[x] and table[(x + 1) // 2]


flag = [judge(x) for x in range(0, 10**5 + 1)]
accmulate(flag)
ans_list = []
for _ in range(Q):
    l, r = map(int, input().split())
    ans = query(l, r + 1)
    ans_list.append(ans)
print(*ans_list, sep="\n")

-import sys - -input = sys.stdin.readline -from itertools import accumulate -import math +def sieve(N): +    global table, prime +    table = [True] * (N + 1) +    table[0] = False +    table[1] = False +    prime = [] +    for i in range(2, N + 1): +        if table[i]: +            prime.append(i) +            for j in range(2 * i, N + 1, i): +                table[j] = False -def is_prime(n): -    if n <= 0: -        return None -    if n == 1: -        return False -    sup = math.floor(pow(n, 1 / 2)) -    for i in range(2, sup + 1): -        if n % i == 0: -            return False -    return True +def accmulate(array): +    global cs +    cs = [0] * (len(array) + 1) +    for i in range(len(array)): +        cs[i + 1] = cs[i] + array[i] -Q = int(eval(input())) -query = [list(map(int, input().split())) for _ in range(Q)] -bit = [0] * (10**5 + 1) -for i in range(10**5 + 1): -    if i % 2 == 1 and is_prime(i) and is_prime((i + 1) // 2): -        bit[i] = 1 -bit = list(accumulate(bit)) -for l, r in query: -    res = bit[r] - bit[l - 1] -    print(res) +def query(l, r): +    return cs[r + 1] - cs[l] + + +Q = int(input()) +sieve(10**5) + + +def judge(x): +    return x % 2 and table[x] and table[(x + 1) // 2] + + +flag = [judge(x) for x in range(0, 10**5 + 1)] +accmulate(flag) +ans_list = [] +for _ in range(Q): +    l, r = map(int, input().split()) +    ans = query(l, r + 1) +    ans_list.append(ans) +print(*ans_list, sep="\n")