# ABC084D - 2017-like Number

import sys

input = sys.stdin.readline



from bisect import bisect_left, bisect_right





def get_primes(n: int) -> list:

    # return a list of primes <= n

    n += 1  # include n itself

    sieve = [1] * n

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

        if sieve[i]:

            sieve[i * i :: 2 * i] = [0] * ((n - i * i - 1) // (2 * i) + 1)

    return [2] + [i for i in range(3, n, 2) if sieve[i]]





def main():

    Q = int(input())

    A = tuple(tuple(map(int, input().split())) for _ in range(Q))

    P = get_primes(10 ** 5)

    S = set(P)

    selected_P = [p for p in P if (p + 1) // 2 in S]

    bsr, bsl = bisect_right, bisect_left

    ans = [bsr(selected_P, r) - bsl(selected_P, l) for l, r in A]

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





if __name__ == "__main__":

    main()

# ABC084D - 2017-like Number

from bisect import bisect_left as bsl, bisect_right as bsr





def get_primes(n: int) -> list:

    # return a list of primes <= n

    sieve = [1] * n

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

        if sieve[i]:

            sieve[i * i :: 2 * i] = [0] * ((n - i * i - 1) // (2 * i) + 1)

    return [2] + [i for i in range(3, n, 2) if sieve[i]]





def main():

    Q, *LR = list(map(int, open(0).read().split()))

    P = get_primes(10 ** 5 + 1)

    S = set(P)

    selected_P = [p for p in P if (p + 1) // 2 in S]  # 2017-like primes

    # the number of 2017-like primes b/w l, r -> bisect[r] - bisect[l]

    ans = [bsr(selected_P, r) - bsl(selected_P, l) for l, r in zip(*[iter(LR)] * 2)]

    print(("\n".join(map(str, ans))))





if __name__ == "__main__":

    main()
# ABC084D - 2017-like Number
import sys

input = sys.stdin.readline
from bisect import bisect_left, bisect_right


def get_primes(n: int) -> list:
    # return a list of primes <= n
    n += 1  # include n itself
    sieve = [1] * n
    for i in range(3, int(n**0.5) + 1, 2):
        if sieve[i]:
            sieve[i * i :: 2 * i] = [0] * ((n - i * i - 1) // (2 * i) + 1)
    return [2] + [i for i in range(3, n, 2) if sieve[i]]


def main():
    Q = int(input())
    A = tuple(tuple(map(int, input().split())) for _ in range(Q))
    P = get_primes(10**5)
    S = set(P)
    selected_P = [p for p in P if (p + 1) // 2 in S]
    bsr, bsl = bisect_right, bisect_left
    ans = [bsr(selected_P, r) - bsl(selected_P, l) for l, r in A]
    print(*ans, sep="\n")


if __name__ == "__main__":
    main()

# ABC084D - 2017-like Number
from bisect import bisect_left as bsl, bisect_right as bsr


def get_primes(n: int) -> list:
    # return a list of primes <= n
    sieve = [1] * n
    for i in range(3, int(n**0.5) + 1, 2):
        if sieve[i]:
            sieve[i * i :: 2 * i] = [0] * ((n - i * i - 1) // (2 * i) + 1)
    return [2] + [i for i in range(3, n, 2) if sieve[i]]


def main():
    Q, *LR = list(map(int, open(0).read().split()))
    P = get_primes(10**5 + 1)
    S = set(P)
    selected_P = [p for p in P if (p + 1) // 2 in S]  # 2017-like primes
    # the number of 2017-like primes b/w l, r -> bisect[r] - bisect[l]
    ans = [bsr(selected_P, r) - bsl(selected_P, l) for l, r in zip(*[iter(LR)] * 2)]
    print(("\n".join(map(str, ans))))


if __name__ == "__main__":
    main()

-import sys - -input = sys.stdin.readline -from bisect import bisect_left, bisect_right +from bisect import bisect_left as bsl, bisect_right as bsr -    n += 1  # include n itself -    Q = int(input()) -    A = tuple(tuple(map(int, input().split())) for _ in range(Q)) -    P = get_primes(10**5) +    Q, *LR = list(map(int, open(0).read().split())) +    P = get_primes(10**5 + 1) -    selected_P = [p for p in P if (p + 1) // 2 in S] -    bsr, bsl = bisect_right, bisect_left -    ans = [bsr(selected_P, r) - bsl(selected_P, l) for l, r in A] -    print(*ans, sep="\n") +    selected_P = [p for p in P if (p + 1) // 2 in S]  # 2017-like primes +    # the number of 2017-like primes b/w l, r -> bisect[r] - bisect[l] +    ans = [bsr(selected_P, r) - bsl(selected_P, l) for l, r in zip(*[iter(LR)] * 2)] +    print(("\n".join(map(str, ans))))