#!/usr/bin/env python3



def main():

    is_prime = sieve(10 ** 5 + 1)

    is_like2017 = [(is_prime[i] and is_prime[(i + 1) // 2]) for i in range(10 ** 5 + 1)]

    counter = [0]

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

        counter.append(counter[-1] + (1 if is_like2017[i] else 0))



    q = int(eval(input()))

    for i in range(q):

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

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





# O(nloglogn) https://mathtrain.jp/eratosthenes

def sieve(n):  # exclusive

    s = [True] * n

    s[0] = s[1] = False

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

        if s[x]:

            for i in range(2 * x, n, x):

                s[i] = False

    return s



if __name__ == "__main__":

    main()

#!/usr/bin/env python3



import sys



def main():

    is_prime = sieve(10 ** 5 + 1)

    is_like2017 = [(is_prime[i] and is_prime[(i + 1) // 2]) for i in range(10 ** 5 + 1)]

    counter = [0]

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

        counter.append(counter[-1] + (1 if is_like2017[i] else 0))



    q = int(eval(input()))

    for i in range(q):

        l, r = list(map(int, sys.stdin.readline().split()))

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





# O(nloglogn) https://mathtrain.jp/eratosthenes

def sieve(n):  # exclusive

    s = [True] * n

    s[0] = s[1] = False

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

        if s[x]:

            for i in range(2 * x, n, x):

                s[i] = False

    return s



if __name__ == "__main__":

    main()

#!/usr/bin/env python3
def main():
    is_prime = sieve(10**5 + 1)
    is_like2017 = [(is_prime[i] and is_prime[(i + 1) // 2]) for i in range(10**5 + 1)]
    counter = [0]
    for i in range(1, 10**5 + 1):
        counter.append(counter[-1] + (1 if is_like2017[i] else 0))
    q = int(eval(input()))
    for i in range(q):
        l, r = list(map(int, input().split()))
        print((counter[r] - counter[l - 1]))


# O(nloglogn) https://mathtrain.jp/eratosthenes
def sieve(n):  # exclusive
    s = [True] * n
    s[0] = s[1] = False
    for x in range(2, int(n**0.5) + 1):
        if s[x]:
            for i in range(2 * x, n, x):
                s[i] = False
    return s


if __name__ == "__main__":
    main()

#!/usr/bin/env python3
import sys


def main():
    is_prime = sieve(10**5 + 1)
    is_like2017 = [(is_prime[i] and is_prime[(i + 1) // 2]) for i in range(10**5 + 1)]
    counter = [0]
    for i in range(1, 10**5 + 1):
        counter.append(counter[-1] + (1 if is_like2017[i] else 0))
    q = int(eval(input()))
    for i in range(q):
        l, r = list(map(int, sys.stdin.readline().split()))
        print((counter[r] - counter[l - 1]))


# O(nloglogn) https://mathtrain.jp/eratosthenes
def sieve(n):  # exclusive
    s = [True] * n
    s[0] = s[1] = False
    for x in range(2, int(n**0.5) + 1):
        if s[x]:
            for i in range(2 * x, n, x):
                s[i] = False
    return s


if __name__ == "__main__":
    main()

+import sys + + -        l, r = list(map(int, input().split())) +        l, r = list(map(int, sys.stdin.readline().split()))