# -*- coding: utf-8 -*-



from math import sqrt



# 素数判定用関数

def is_prime2(num):

    if num < 2:

        return False

    if num == 2 or num == 3 or num == 5:

        return True

    if num % 2 == 0 or num % 3 == 0 or num % 5 == 0:

        return False

    # 疑似素数(2でも3でも割り切れない数字)で次々に割っていく

    prime = 7

    step = 4

    num_sqrt = sqrt(num)

    while prime <= num_sqrt:

        if num % prime == 0:

            return False

        prime += step

        step = 6 - step

    return True



# 同じ数2回素数判定やりたくないのでここに保存

prime = [is_prime2(i) for i in range(100001)]

# 2017に似た数

like2017 = [0] * 100001

for i in range(1, 100001, 2):

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

        like2017[i] = 1

# 累積和

sm = [0] * 100001

for i in range(1, 100001):

    sm[i] = sm[i-1] + like2017[i]



Q = int(eval(input()))

for i in range(Q):

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

    # 累積和から各QはO(1)で出せる

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

# -*- coding: utf-8 -*-



import sys

from itertools import accumulate



def input(): return sys.stdin.readline().strip()

def list2d(a, b, c): return [[c] * b for i in range(a)]

def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1): return int(-(-x // y))

def INT(): return int(eval(input()))

def MAP(): return list(map(int, input().split()))

def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes(): print('Yes')

def No(): print('No')

def YES(): print('YES')

def NO(): print('NO')

sys.setrecursionlimit(10 ** 9)

INF = float('inf')

MOD = 10 ** 9 + 7



def eratosthenes_sieve(n):

    """ 素数列挙(エラトステネスの篩) """



    table = [0] * (n + 1)

    prime_list = []

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

        if table[i] == 0:

            prime_list.append(i)

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

                table[j] = 1

    return prime_list



N = 100000

primes = set(eratosthenes_sieve(N+1))



A = [0] * (N+1)

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

    if a in primes and (a+1)//2 in primes:

        A[a] = 1



acc = [0] + list(accumulate(A))

for i in range(INT()):

    l, r = MAP()

    ans = acc[r+1] - acc[l]

    print(ans)

# -*- coding: utf-8 -*-
from math import sqrt

# 素数判定用関数
def is_prime2(num):
    if num < 2:
        return False
    if num == 2 or num == 3 or num == 5:
        return True
    if num % 2 == 0 or num % 3 == 0 or num % 5 == 0:
        return False
    # 疑似素数(2でも3でも割り切れない数字)で次々に割っていく
    prime = 7
    step = 4
    num_sqrt = sqrt(num)
    while prime <= num_sqrt:
        if num % prime == 0:
            return False
        prime += step
        step = 6 - step
    return True


# 同じ数2回素数判定やりたくないのでここに保存
prime = [is_prime2(i) for i in range(100001)]
# 2017に似た数
like2017 = [0] * 100001
for i in range(1, 100001, 2):
    if prime[i] and prime[(i + 1) // 2]:
        like2017[i] = 1
# 累積和
sm = [0] * 100001
for i in range(1, 100001):
    sm[i] = sm[i - 1] + like2017[i]
Q = int(eval(input()))
for i in range(Q):
    l, r = list(map(int, input().split()))
    # 累積和から各QはO(1)で出せる
    print((sm[r] - sm[l - 1]))

# -*- coding: utf-8 -*-
import sys
from itertools import accumulate


def input():
    return sys.stdin.readline().strip()


def list2d(a, b, c):
    return [[c] * b for i in range(a)]


def list3d(a, b, c, d):
    return [[[d] * c for j in range(b)] for i in range(a)]


def list4d(a, b, c, d, e):
    return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]


def ceil(x, y=1):
    return int(-(-x // y))


def INT():
    return int(eval(input()))


def MAP():
    return list(map(int, input().split()))


def LIST(N=None):
    return list(MAP()) if N is None else [INT() for i in range(N)]


def Yes():
    print("Yes")


def No():
    print("No")


def YES():
    print("YES")


def NO():
    print("NO")


sys.setrecursionlimit(10**9)
INF = float("inf")
MOD = 10**9 + 7


def eratosthenes_sieve(n):
    """素数列挙(エラトステネスの篩)"""
    table = [0] * (n + 1)
    prime_list = []
    for i in range(2, n + 1):
        if table[i] == 0:
            prime_list.append(i)
            for j in range(i + i, n + 1, i):
                table[j] = 1
    return prime_list


N = 100000
primes = set(eratosthenes_sieve(N + 1))
A = [0] * (N + 1)
for a in range(1, N + 1, 2):
    if a in primes and (a + 1) // 2 in primes:
        A[a] = 1
acc = [0] + list(accumulate(A))
for i in range(INT()):
    l, r = MAP()
    ans = acc[r + 1] - acc[l]
    print(ans)

-from math import sqrt - -# 素数判定用関数 -def is_prime2(num): -    if num < 2: -        return False -    if num == 2 or num == 3 or num == 5: -        return True -    if num % 2 == 0 or num % 3 == 0 or num % 5 == 0: -        return False -    # 疑似素数(2でも3でも割り切れない数字)で次々に割っていく -    prime = 7 -    step = 4 -    num_sqrt = sqrt(num) -    while prime <= num_sqrt: -        if num % prime == 0: -            return False -        prime += step -        step = 6 - step -    return True +import sys +from itertools import accumulate -# 同じ数2回素数判定やりたくないのでここに保存 -prime = [is_prime2(i) for i in range(100001)] -# 2017に似た数 -like2017 = [0] * 100001 -for i in range(1, 100001, 2): -    if prime[i] and prime[(i + 1) // 2]: -        like2017[i] = 1 -# 累積和 -sm = [0] * 100001 -for i in range(1, 100001): -    sm[i] = sm[i - 1] + like2017[i] -Q = int(eval(input())) -for i in range(Q): -    l, r = list(map(int, input().split())) -    # 累積和から各QはO(1)で出せる -    print((sm[r] - sm[l - 1])) +def input(): +    return sys.stdin.readline().strip() + + +def list2d(a, b, c): +    return [[c] * b for i in range(a)] + + +def list3d(a, b, c, d): +    return [[[d] * c for j in range(b)] for i in range(a)] + + +def list4d(a, b, c, d, e): +    return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] + + +def ceil(x, y=1): +    return int(-(-x // y)) + + +def INT(): +    return int(eval(input())) + + +def MAP(): +    return list(map(int, input().split())) + + +def LIST(N=None): +    return list(MAP()) if N is None else [INT() for i in range(N)] + + +def Yes(): +    print("Yes") + + +def No(): +    print("No") + + +def YES(): +    print("YES") + + +def NO(): +    print("NO") + + +sys.setrecursionlimit(10**9) +INF = float("inf") +MOD = 10**9 + 7 + + +def eratosthenes_sieve(n): +    """素数列挙(エラトステネスの篩)""" +    table = [0] * (n + 1) +    prime_list = [] +    for i in range(2, n + 1): +        if table[i] == 0: +            prime_list.append(i) +            for j in range(i + i, n + 1, i): +                table[j] = 1 +    return prime_list + + +N = 100000 +primes = set(eratosthenes_sieve(N + 1)) +A = [0] * (N + 1) +for a in range(1, N + 1, 2): +    if a in primes and (a + 1) // 2 in primes: +        A[a] = 1 +acc = [0] + list(accumulate(A)) +for i in range(INT()): +    l, r = MAP() +    ans = acc[r + 1] - acc[l] +    print(ans)