Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
q = int(input())


p_max = 10**5+1
m = np.zeros(p_max, dtype=int)
for i in range(2, p_max):
    m[::i] += 1
# print(m)
ans = []
for _ in range(q):
    cnt = 0
    l, r = map(int, input().split())
    for i in range(l, r+1, 2):
        if m[i] == 1 and m[(i+1)//2] == 1:
            cnt += 1
    ans.append(cnt)


for i in range(q):
    print(ans[i])

Fixed submission:
n_max = 10 ** 5 + 10
sieve = [0, 0] + [1]*n_max
m = 2
while m * m <= n_max:
    if sieve[m]:
        for n in range(2*m, n_max, m):
            sieve[n] = 0
    m += 1

ok = [0]*n_max
cums = [0]
for n in range(n_max):
    if sieve[n] and sieve[(n+1)//2]:
        ok[n] = 1
    cums.append(cums[-1]+ok[n])

q = int(input())
for i in range(q):
    l, r = map(int, input().split())
    print(cums[r+1]-cums[l])

Buggy python submission (verdict: Time Limit Exceeded):
q = int(input())


def isPrime(n):
    if n == 1:
        return 0
    if n == 2:
        return 1
    if n % 2 == 0:
        return 0
    m = int(n**0.5)+1
    for p in range(3, m, 2):
        if n % p == 0:
            return 0
    return 1


prime = [0]*(10**5+1)
for i in range(1, 10**5+1, 2):
    prime[i] = isPrime(i) & isPrime((i+1)//2)
# print(prime)

ans = []
for _ in range(q):
    l, r = map(int, input().split())
    cnt = sum(prime[l:r+1])
    ans.append(cnt)


for i in range(q):
    print(ans[i])

Fixed submission:
n_max = 10 ** 5 + 10
sieve = [0, 0] + [1]*n_max
m = 2
while m * m <= n_max:
    if sieve[m]:
        for n in range(2*m, n_max, m):
            sieve[n] = 0
    m += 1

ok = [0]*n_max
cums = [0]
for n in range(n_max):
    if sieve[n] and sieve[(n+1)//2]:
        ok[n] = 1
    cums.append(cums[-1]+ok[n])

q = int(input())
for i in range(q):
    l, r = map(int, input().split())
    print(cums[r+1]-cums[l])

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    q = int(input())
    
    
    def isPrime(n):
        if n == 1:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        m = int(n**0.5)+1
        for p in range(3, m, 2):
            if n % p == 0:
                return False
        return True
    
    
    ans = []
    for _ in range(q):
        l, r = map(int, input().split())
        cnt = 0
        for i in range(l, r+1, 2):
            if isPrime(i) and isPrime((i+1)//2):
                cnt += 1
        ans.append(cnt)
    
    for i in range(q):
        print(ans[i])

main()

Fixed submission:
n_max = 10 ** 5 + 10
sieve = [0, 0] + [1]*n_max
m = 2
while m * m <= n_max:
    if sieve[m]:
        for n in range(2*m, n_max, m):
            sieve[n] = 0
    m += 1

ok = [0]*n_max
cums = [0]
for n in range(n_max):
    if sieve[n] and sieve[(n+1)//2]:
        ok[n] = 1
    cums.append(cums[-1]+ok[n])

q = int(input())
for i in range(q):
    l, r = map(int, input().split())
    print(cums[r+1]-cums[l])

Buggy python submission (verdict: Time Limit Exceeded):
q = int(input())


def isPrime(n):
    if n == 1:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    m = int(n**0.5)+1
    for p in range(3, m, 2):
        if n % p == 0:
            return False
    return True


ans = []
for _ in range(q):
    l, r = map(int, input().split())
    cnt = 0
    for i in range(l, r+1, 2):
        if isPrime(i) and isPrime((i+1)//2):
            cnt += 1
    ans.append(cnt)

for i in range(q):
    print(ans[i])

Fixed submission:
n_max = 10 ** 5 + 10
sieve = [0, 0] + [1]*n_max
m = 2
while m * m <= n_max:
    if sieve[m]:
        for n in range(2*m, n_max, m):
            sieve[n] = 0
    m += 1

ok = [0]*n_max
cums = [0]
for n in range(n_max):
    if sieve[n] and sieve[(n+1)//2]:
        ok[n] = 1
    cums.append(cums[-1]+ok[n])

q = int(input())
for i in range(q):
    l, r = map(int, input().split())
    print(cums[r+1]-cums[l])