Buggy python submission (verdict: Time Limit Exceeded):
import math
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
MAX = 100000

def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []
    time = 0

    while True:
        prime = min(A)

        if prime > math.sqrt(limit):
            break

        P.append(prime)
        inP_list[prime] = True

        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1

    for a in A:
        P.append(a)
        inP_list[a]=True

    return P

inP_list = [False]*MAX
p_list=eratosthenes(MAX)

ans_sum_list = [0] * MAX

for i in range(3,MAX, 2):
    if inP_list[i] and inP_list[(i+1)//2]:
        ans_sum_list[i] += 1
    ans_sum_list[i] += ans_sum_list[i-2]

q = int(input())
ans = [0]*q
for i in range(q):
    l, r = map(int, input().split())
    ans[i] = ans_sum_list[r] - ans_sum_list[max(1,l-2)]

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


Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

q = int(input())

def eratosthenes2(limit):
    is_prime = [1] * (limit + 1)
    is_prime[0] = 0
    is_prime[1] = 0
    for i in range(2, int(limit**0.5) + 1):
        if is_prime[i]==0:
            continue
        for j in range(i * 2, limit + 1, i):
            is_prime[j] = 0
    return is_prime

limit = 10**5

is_prime = eratosthenes2(limit)
prime_list = [i for i in range(limit + 1) if is_prime[i]]

like_2017=[0]*limit

for p in prime_list:
    if is_prime[(p+1)//2]==1:
        like_2017[p] = 1

from itertools import accumulate
B = [0] + like_2017
B = list(accumulate(B))

ans=[0]*q
for i in range(q):
    l, r = map(int, input().split())
    ans[i]=B[r+1]-B[l]

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

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

def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []
    time = 0

    while True:
        prime = min(A)

        if prime > math.sqrt(limit):
            break

        P.append(prime)

        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1

    for a in A:
        P.append(a)

    return P

p_list=eratosthenes(10000)

import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

q = int(input())
ans = [0]*q
for i in range(q):
    l, r = map(int, input().split())
    tmp = 0
    for j in range (l,r+1,2):
        if j in p_list and (j+1)//2 in p_list: tmp+=1
    ans[i] = tmp

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


Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

q = int(input())

def eratosthenes2(limit):
    is_prime = [1] * (limit + 1)
    is_prime[0] = 0
    is_prime[1] = 0
    for i in range(2, int(limit**0.5) + 1):
        if is_prime[i]==0:
            continue
        for j in range(i * 2, limit + 1, i):
            is_prime[j] = 0
    return is_prime

limit = 10**5

is_prime = eratosthenes2(limit)
prime_list = [i for i in range(limit + 1) if is_prime[i]]

like_2017=[0]*limit

for p in prime_list:
    if is_prime[(p+1)//2]==1:
        like_2017[p] = 1

from itertools import accumulate
B = [0] + like_2017
B = list(accumulate(B))

ans=[0]*q
for i in range(q):
    l, r = map(int, input().split())
    ans[i]=B[r+1]-B[l]

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