Buggy python submission (verdict: Time Limit Exceeded):
def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return is_prime

is_prime = primes(10**5)

Q = int(input())

ans = []
for i in range(Q):
    l, r = map(int, input().split())

    cnt = 0
    for j in range(l, r+1):
        if is_prime[j] and is_prime[(j+1)//2]:
            cnt += 1
    ans.append(cnt)

for a in ans:
    print(a)


Fixed submission:
def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return is_prime

is_prime = primes(10**5)
like = [False] * 10**5
for i in range(3, 10**5, 2):
    if is_prime[i] and is_prime[(i+1)//2]:
        like[i] = True

cusum = [0] * 10**5
for i in range(3, 10**5, 2):
    cusum[i] = cusum[i-2] + like[i]

Q = int(input())
for i in range(Q):
    l, r = map(int, input().split())

    if l == 1:
        print(cusum[r])
    else:
        print(cusum[r] - cusum[l-2])

Buggy python submission (verdict: Time Limit Exceeded):
def is_prime(n):
    if n == 1: return False

    for k in range(2, int(n**(1/2)) + 1):
        if n % k == 0:
            return False

    return True

Q = int(input())

ans = []
for i in range(Q):
    l, r = map(int, input().split())

    cnt = 0
    for j in range(l, r+1):
        if is_prime(j) and is_prime((j+1)//2):
            cnt += 1
    ans.append(cnt)

for a in ans:
    print(a)


Fixed submission:
def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return is_prime

is_prime = primes(10**5)
like = [False] * 10**5
for i in range(3, 10**5, 2):
    if is_prime[i] and is_prime[(i+1)//2]:
        like[i] = True

cusum = [0] * 10**5
for i in range(3, 10**5, 2):
    cusum[i] = cusum[i-2] + like[i]

Q = int(input())
for i in range(Q):
    l, r = map(int, input().split())

    if l == 1:
        print(cusum[r])
    else:
        print(cusum[r] - cusum[l-2])

Buggy python submission (verdict: Time Limit Exceeded):
def is_prime(n):
    if n == 1: return False

    for k in range(2, int(n**(1/2)) + 1):
        if n % k == 0:
            return False

    return True

Q = int(input())

ans = []
for i in range(Q):
    l, r = map(int, input().split())

    cnt = 0
    for j in range(l, r+1):
        if is_prime(j) and is_prime((j+1)//2):
            cnt += 1
    ans.append(cnt)

for a in ans:
    print(a)


Fixed submission:
def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return is_prime

is_prime = primes(10**5)
like = [False] * 10**5
for i in range(3, 10**5, 2):
    if is_prime[i] and is_prime[(i+1)//2]:
        like[i] = True

cusum = [0] * 10**5
for i in range(3, 10**5, 2):
    cusum[i] = cusum[i-2] + like[i]

Q = int(input())
for i in range(Q):
    l, r = map(int, input().split())

    if l == 1:
        print(cusum[r])
    else:
        print(cusum[r] - cusum[l-2])