from bisect import bisect_left, bisect

def sieve_of_eratosthenes_2017(N):

    numbers = [n & 1 for n in range(N + 1)]

    numbers[1] = 0

    numbers[2] = 1

    like2017 = []

    p = 1

    while True:

        while not numbers[p]:

            p += 2

        if p * p > N:

            for i in range(p, N + 1, 2):

                if numbers[i] & numbers[(i + 1) >> 1]:

                    like2017.append(i)

            return like2017

        if numbers[(p + 1) >> 1]:

            like2017.append(p)

        for i in range(p * 2, N + 1, p):

            numbers[i] = 0

        p += 2



like2017 = sieve_of_eratosthenes_2017(10 ** 5)

Q = int(eval(input()))

for _ in range(Q):

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

    lindex = bisect_left(like2017, l)

    rindex = bisect(like2017, r)

    print((rindex - lindex))
from itertools import accumulate

def sieve_of_eratosthenes_2017(N):

    numbers = [n & 1 for n in range(N + 1)]

    numbers[1] = 0

    numbers[2] = 1

    like2017 = [0] * (N + 1)

    p = 1

    while True:

        while not numbers[p]:

            p += 2

        if p * p > N:

            for i in range(p, N + 1, 2):

                if numbers[i] & numbers[(i + 1) >> 1]:

                    like2017[i] = 1

            return like2017

        if numbers[(p + 1) >> 1]:

            like2017[p] = 1

        for i in range(p * 2, N + 1, p):

            numbers[i] = 0

        p += 2



like2017 = sieve_of_eratosthenes_2017(10 ** 5)

like2017_sum = list(accumulate(like2017))

Q = int(eval(input()))

for _ in range(Q):

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

    print((like2017_sum[r] - like2017_sum[l] + like2017[l]))

from bisect import bisect_left, bisect


def sieve_of_eratosthenes_2017(N):
    numbers = [n & 1 for n in range(N + 1)]
    numbers[1] = 0
    numbers[2] = 1
    like2017 = []
    p = 1
    while True:
        while not numbers[p]:
            p += 2
        if p * p > N:
            for i in range(p, N + 1, 2):
                if numbers[i] & numbers[(i + 1) >> 1]:
                    like2017.append(i)
            return like2017
        if numbers[(p + 1) >> 1]:
            like2017.append(p)
        for i in range(p * 2, N + 1, p):
            numbers[i] = 0
        p += 2


like2017 = sieve_of_eratosthenes_2017(10**5)
Q = int(eval(input()))
for _ in range(Q):
    l, r = list(map(int, input().split()))
    lindex = bisect_left(like2017, l)
    rindex = bisect(like2017, r)
    print((rindex - lindex))

from itertools import accumulate


def sieve_of_eratosthenes_2017(N):
    numbers = [n & 1 for n in range(N + 1)]
    numbers[1] = 0
    numbers[2] = 1
    like2017 = [0] * (N + 1)
    p = 1
    while True:
        while not numbers[p]:
            p += 2
        if p * p > N:
            for i in range(p, N + 1, 2):
                if numbers[i] & numbers[(i + 1) >> 1]:
                    like2017[i] = 1
            return like2017
        if numbers[(p + 1) >> 1]:
            like2017[p] = 1
        for i in range(p * 2, N + 1, p):
            numbers[i] = 0
        p += 2


like2017 = sieve_of_eratosthenes_2017(10**5)
like2017_sum = list(accumulate(like2017))
Q = int(eval(input()))
for _ in range(Q):
    l, r = list(map(int, input().split()))
    print((like2017_sum[r] - like2017_sum[l] + like2017[l]))

-from bisect import bisect_left, bisect +from itertools import accumulate -    like2017 = [] +    like2017 = [0] * (N + 1) -                    like2017.append(i) +                    like2017[i] = 1 -            like2017.append(p) +            like2017[p] = 1 +like2017_sum = list(accumulate(like2017)) -    lindex = bisect_left(like2017, l) -    rindex = bisect(like2017, r) -    print((rindex - lindex)) +    print((like2017_sum[r] - like2017_sum[l] + like2017[l]))