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

def solve(l,r,c):
    return c[r]-c[l-1]



if __name__ == '__main__':
    q = int(input())
    for i in range(q):
        l,r = map(int,input().split())
        c = sieve(100000)
        print(solve(l,r,c))

Fixed submission:
def sieve(n):
    is_prime =[True for i in range(n+1)]
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2,int(n**0.5)+1):
        if is_prime[i]:
            for j in range(i+i,n,i):
                is_prime[j] = False
    c = [0 for i in range(n+1)]
    for i in range(3,n+1,2):
        if is_prime[i] and is_prime[(i+1)//2]:
            c[i] = 1
    for i in range(n):
        c[i+1] += c[i]
    return c

def solve(l,r,c):
    return c[r]-c[l-1]



if __name__ == '__main__':
    q = int(input())
    c = sieve(100000)
    for i in range(q):
        l,r = map(int,input().split())
        print(solve(l,r,c))

Buggy python submission (verdict: Time Limit Exceeded):
def sieve(n):
    is_prime =[True for i in range(n+1)]
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2,n+1):
        if is_prime[i]:
            for j in range(i+i,n,i):
                is_prime[j] = False
    c = [0 for i in range(n+1)]
    for i in range(3,n+1,2):
        if is_prime[i] and is_prime[(i+1)//2]:
            c[i] = 1
    for i in range(n):
        c[i+1] += c[i]
    return c

def solve(l,r,c):
    return c[r]-c[l-1]



if __name__ == '__main__':
    q = int(input())
    for i in range(q):
        l,r = map(int,input().split())
        c = sieve(100000)
        print(solve(l,r,c))

Fixed submission:
def sieve(n):
    is_prime =[True for i in range(n+1)]
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2,int(n**0.5)+1):
        if is_prime[i]:
            for j in range(i+i,n,i):
                is_prime[j] = False
    c = [0 for i in range(n+1)]
    for i in range(3,n+1,2):
        if is_prime[i] and is_prime[(i+1)//2]:
            c[i] = 1
    for i in range(n):
        c[i+1] += c[i]
    return c

def solve(l,r,c):
    return c[r]-c[l-1]



if __name__ == '__main__':
    q = int(input())
    c = sieve(100000)
    for i in range(q):
        l,r = map(int,input().split())
        print(solve(l,r,c))

Buggy python submission (verdict: Time Limit Exceeded):
def solve(l,r):
    p = 0
    is_prime =[True for i in range(r+1)]
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2,r+1):
        if is_prime[i]:
            p += 1
            for j in range(i+i,r,i):
                is_prime[j] = False
    c = [0 for i in range(r+1)]
    for i in range(3,r+1,2):
        if is_prime[i] and is_prime[(i+1)//2]:
            c[i] = c[i-2] + 1
        else:
            c[i] = c[i-2]
    if l != 1:
        res = c[r] - c[l-2]
    else:
        res = c[r] - c[l]
    return res

if __name__ == '__main__':
    q = int(input())
    for i in range(q):
        l,r = map(int,input().split())
        print(solve(l,r))

Fixed submission:
def sieve(n):
    is_prime =[True for i in range(n+1)]
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2,int(n**0.5)+1):
        if is_prime[i]:
            for j in range(i+i,n,i):
                is_prime[j] = False
    c = [0 for i in range(n+1)]
    for i in range(3,n+1,2):
        if is_prime[i] and is_prime[(i+1)//2]:
            c[i] = 1
    for i in range(n):
        c[i+1] += c[i]
    return c

def solve(l,r,c):
    return c[r]-c[l-1]



if __name__ == '__main__':
    q = int(input())
    c = sieve(100000)
    for i in range(q):
        l,r = map(int,input().split())
        print(solve(l,r,c))

Buggy python submission (verdict: Time Limit Exceeded):
def solve(l,r):
    prime = [True for i in range(r+1)]
    prime[0] = False
    prime[1] = False
    for i in range(2,int(r**0.5)+1):
        for j in range(2*i,r+1,i):
            prime[j] = False
    res = 0
    for i in range(l,r+1,2):
        if prime[i] and prime[(i+1)//2]:
            res += 1
    return res

if __name__ == '__main__':
    q = int(input())
    for i in range(q):
        l,r = map(int,input().split())
        print(solve(l,r))

Fixed submission:
def sieve(n):
    is_prime =[True for i in range(n+1)]
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2,int(n**0.5)+1):
        if is_prime[i]:
            for j in range(i+i,n,i):
                is_prime[j] = False
    c = [0 for i in range(n+1)]
    for i in range(3,n+1,2):
        if is_prime[i] and is_prime[(i+1)//2]:
            c[i] = 1
    for i in range(n):
        c[i+1] += c[i]
    return c

def solve(l,r,c):
    return c[r]-c[l-1]



if __name__ == '__main__':
    q = int(input())
    c = sieve(100000)
    for i in range(q):
        l,r = map(int,input().split())
        print(solve(l,r,c))