def primes(n):

    is_prime = [True] * (n + 1)

    is_prime[0] = False

    is_prime[1] = False

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

        if not is_prime[i]:

            continue

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

            is_prime[j] = False

    return is_prime

Q = int(eval(input()))

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

prime = primes(10**5)

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

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

        ans[i] = ans[i-1] + 1

        continue

    ans[i] = ans[i-1]

for i in range(Q):

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

    print((ans[r]-ans[l-1]))
import math

def is_prime(n):

    if n == 1: return False

    for k in range(2, int(math.sqrt(n)) + 1):

        if n % k == 0:

            return False

    return True

Q = int(eval(input()))

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

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

    if i % 2 == 0:

        cnt[i] = cnt[i-1]

        continue

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

        cnt[i] = cnt[i-1] + 1

    else:

        cnt[i] = cnt[i-1]

for _ in range(Q):

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

    print((cnt[r]-cnt[l-1]))
def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return is_prime


Q = int(eval(input()))
ans = [0] * (10**5 + 1)
prime = primes(10**5)
for i in range(1, 10**5 + 1):
    if prime[i] == 1 and prime[(i + 1) // 2] == 1:
        ans[i] = ans[i - 1] + 1
        continue
    ans[i] = ans[i - 1]
for i in range(Q):
    l, r = list(map(int, input().split()))
    print((ans[r] - ans[l - 1]))

import math


def is_prime(n):
    if n == 1:
        return False
    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False
    return True


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

-def primes(n): -    is_prime = [True] * (n + 1) -    is_prime[0] = False -    is_prime[1] = False -    for i in range(2, int(n**0.5) + 1): -        if not is_prime[i]: -            continue -        for j in range(i * 2, n + 1, i): -            is_prime[j] = False -    return is_prime +import math + + +def is_prime(n): +    if n == 1: +        return False +    for k in range(2, int(math.sqrt(n)) + 1): +        if n % k == 0: +            return False +    return True -ans = [0] * (10**5 + 1) -prime = primes(10**5) +cnt = [0] * (10**5 + 1) -    if prime[i] == 1 and prime[(i + 1) // 2] == 1: -        ans[i] = ans[i - 1] + 1 +    if i % 2 == 0: +        cnt[i] = cnt[i - 1] -    ans[i] = ans[i - 1] -for i in range(Q): +    if is_prime(i) and is_prime((i + 1) // 2): +        cnt[i] = cnt[i - 1] + 1 +    else: +        cnt[i] = cnt[i - 1] +for _ in range(Q): -    print((ans[r] - ans[l - 1])) +    print((cnt[r] - cnt[l - 1]))