from math import sqrt

q = int(eval(input()))



primes = {i for i in range(2,10**5 + 1)}

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

    if i in primes:

        mul = 2

        while i*mul <= 10**5:

            primes.discard(i*mul)

            mul += 1



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

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

    if i&1:

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

            ret[i] = ret[i-1] + 1

        else:

            ret[i] = ret[i-1]

    else:

        ret[i] = ret[i-1]



for i in range(q):

    l, r = (int(i) for i in input().split())

    print((ret[r] - ret[l-1]))
def main():

    from itertools import accumulate



    def Eratosthenes(x: int) -> set:

        from math import sqrt

        sup = int(x)

        primes = {i for i in range(2, sup+1)}

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

            if i in primes:

                mul = 2

                while i*mul <= sup:

                    primes.discard(i*mul)

                    mul += 1

        return primes

    P = Eratosthenes(10**5+5)

    A = [0]*(10**5+5)

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

        if i in P and (i+1)//2 in P:

            A[i] += 1

    S = list(accumulate(A))



    Q = int(eval(input()))

    LR = [[int(i) for i in input().split()] for j in range(Q)]

    for le, ri in LR:

        print((S[ri] - S[le-1]))





if __name__ == '__main__':

    main()

from math import sqrt

q = int(eval(input()))
primes = {i for i in range(2, 10**5 + 1)}
for i in range(2, int(sqrt(10**5 + 1)) + 1):
    if i in primes:
        mul = 2
        while i * mul <= 10**5:
            primes.discard(i * mul)
            mul += 1
ret = [0] * (1 + 10**5)
for i in range(3, 1 + 10**5):
    if i & 1:
        if i in primes and (i + 1) / 2 in primes:
            ret[i] = ret[i - 1] + 1
        else:
            ret[i] = ret[i - 1]
    else:
        ret[i] = ret[i - 1]
for i in range(q):
    l, r = (int(i) for i in input().split())
    print((ret[r] - ret[l - 1]))

def main():
    from itertools import accumulate

    def Eratosthenes(x: int) -> set:
        from math import sqrt

        sup = int(x)
        primes = {i for i in range(2, sup + 1)}
        for i in range(2, int(sqrt(sup + 1)) + 1):
            if i in primes:
                mul = 2
                while i * mul <= sup:
                    primes.discard(i * mul)
                    mul += 1
        return primes

    P = Eratosthenes(10**5 + 5)
    A = [0] * (10**5 + 5)
    for i in range(3, 10**5 + 5):
        if i in P and (i + 1) // 2 in P:
            A[i] += 1
    S = list(accumulate(A))
    Q = int(eval(input()))
    LR = [[int(i) for i in input().split()] for j in range(Q)]
    for le, ri in LR:
        print((S[ri] - S[le - 1]))


if __name__ == "__main__":
    main()

-from math import sqrt +def main(): +    from itertools import accumulate -q = int(eval(input())) -primes = {i for i in range(2, 10**5 + 1)} -for i in range(2, int(sqrt(10**5 + 1)) + 1): -    if i in primes: -        mul = 2 -        while i * mul <= 10**5: -            primes.discard(i * mul) -            mul += 1 -ret = [0] * (1 + 10**5) -for i in range(3, 1 + 10**5): -    if i & 1: -        if i in primes and (i + 1) / 2 in primes: -            ret[i] = ret[i - 1] + 1 -        else: -            ret[i] = ret[i - 1] -    else: -        ret[i] = ret[i - 1] -for i in range(q): -    l, r = (int(i) for i in input().split()) -    print((ret[r] - ret[l - 1])) +    def Eratosthenes(x: int) -> set: +        from math import sqrt + +        sup = int(x) +        primes = {i for i in range(2, sup + 1)} +        for i in range(2, int(sqrt(sup + 1)) + 1): +            if i in primes: +                mul = 2 +                while i * mul <= sup: +                    primes.discard(i * mul) +                    mul += 1 +        return primes + +    P = Eratosthenes(10**5 + 5) +    A = [0] * (10**5 + 5) +    for i in range(3, 10**5 + 5): +        if i in P and (i + 1) // 2 in P: +            A[i] += 1 +    S = list(accumulate(A)) +    Q = int(eval(input())) +    LR = [[int(i) for i in input().split()] for j in range(Q)] +    for le, ri in LR: +        print((S[ri] - S[le - 1])) + + +if __name__ == "__main__": +    main()