Buggy python submission (verdict: Time Limit Exceeded):
import math

def eratosthenes(limit):
    A = [i for i in range(2, limit + 1)]
    B = []
    C = [False for _ in range(limit + 4)]
    while True:
        a = min(A)
        if a > math.sqrt(limit):
            break
        B.append(a)
        i = 0
        while len(A) > i:
            if A[i] % a == 0:
                A.pop(i)
                continue
            i += 1
    for a in A:
        B.append(a)
    for b in B:
        C[b] = True
    return C

def main():
    Q = int(input())
    lr = []
    mr = 0
    ans = []
    for _ in range(Q):
        l, r = map(int, input().split())
        lr.append((l, r))
        mr = max(mr, r)
    A = eratosthenes(mr)
    for l, r in lr:
        c = 0
        for i in range(l, r + 1, 2):
            if A[i] and A[(i + 1) // 2]:
                c += 1
        ans.append(c)
    for c in ans:
        print(c)


main()


Fixed submission:
import math

def eratosthenes():
    limit = 100001
    A = [i for i in range(2, limit)]
    B = [False for _ in range(limit + 1)]
    C = [0 for i in range(limit + 1)]
    while True:
        a = min(A)
        if a > math.sqrt(limit):
            break
        B[a] = True
        i = 0
        while len(A) > i:
            if A[i] % a == 0:
                A.pop(i)
                continue
            i += 1
    for a in A:
        B[a] = True
    for i in range(3, limit + 1, 2):
        if B[i] and B[(i + 1) // 2]:
            C[i] += 1
    for i in range(3, limit + 1):
        C[i] += C[i - 1]
    return C


def main():
    Q = int(input())
    C = eratosthenes()
    ans = []
    for _ in range(Q):
        l, r = map(int, input().split())
        ans.append(C[r] - C[l - 1])
    for c in ans:
        print(c)


main()

Buggy python submission (verdict: Time Limit Exceeded):
import math

def eratosthenes(limit):
    A = [i for i in range(2, limit + 1)]
    B = []
    while True:
        a = min(A)
        if a > math.sqrt(limit):
            break
        B.append(a)
        i = 0
        while len(A) > i:
            if A[i] % a == 0:
                A.pop(i)
                continue
            i += 1
    for a in A:
        B.append(a)
    return B

def main():
    Q = int(input())
    ans = []
    for _ in range(Q):
        l, r = map(int, input().split())
        A = eratosthenes(r + 1)
        c = 0
        for i in range(l, r + 1):
            if i in A and (i + 1) // 2 in A:
                c += 1
        ans.append(c)
    for c in ans:
        print(c)


main()


Fixed submission:
import math

def eratosthenes():
    limit = 100001
    A = [i for i in range(2, limit)]
    B = [False for _ in range(limit + 1)]
    C = [0 for i in range(limit + 1)]
    while True:
        a = min(A)
        if a > math.sqrt(limit):
            break
        B[a] = True
        i = 0
        while len(A) > i:
            if A[i] % a == 0:
                A.pop(i)
                continue
            i += 1
    for a in A:
        B[a] = True
    for i in range(3, limit + 1, 2):
        if B[i] and B[(i + 1) // 2]:
            C[i] += 1
    for i in range(3, limit + 1):
        C[i] += C[i - 1]
    return C


def main():
    Q = int(input())
    C = eratosthenes()
    ans = []
    for _ in range(Q):
        l, r = map(int, input().split())
        ans.append(C[r] - C[l - 1])
    for c in ans:
        print(c)


main()