Buggy python submission (verdict: Time Limit Exceeded):
import copy
Q = int(input())
lis = []
minimam = 10**5
maximam = 1
for _ in range(Q):
    l, r = map(int, input().split())
    lis.append((l, r))
    minimam = min(minimam, l)
    maximam = max(maximam, r)
flag_lis = [1] * (maximam+1)
flag_lis[0] = 0
flag_lis[1] = 0
for i in range(2, int((maximam-1)/2)+1):
    if flag_lis[i] != 0:
        for j in range(2*i,maximam+1,i):
            flag_lis[j] = 0
fin_lis = copy.copy(flag_lis)
for i in range(2, int((maximam+1)/2)+1):
    if flag_lis[i] == 0:
        fin_lis[2*i-1] = 0
for (l, r) in lis:
    count = 0
    for num in range(l,r+1,2):
        if fin_lis[num]==1:
            count += 1
    print(count)

Fixed submission:
import copy
Q = int(input())
lis = []
maximam = 1
for _ in range(Q):
    l, r = map(int, input().split())
    lis.append((l, r))
    maximam = max(maximam, r)
flag_lis = [1] * (maximam+1)
flag_lis[0] = 0
flag_lis[1] = 0
for i in range(2, int((maximam-1)/2)+1):
    if flag_lis[i] != 0:
        for j in range(2*i,maximam+1,i):
            flag_lis[j] = 0
fin_lis = [0]*(maximam+1)
count = 0
for i in range(3, maximam+1, 2):
    if flag_lis[i] == 1 and flag_lis[int((i+1)/2)] == 1:
        count += 1
    fin_lis[i] = count
for (l, r) in lis:
    if l == 1:
        print(fin_lis[r] - 0)
    else:
        print(fin_lis[r] - fin_lis[l-2])

Buggy python submission (verdict: Time Limit Exceeded):
def sosucheck(num):
    if num == 1:return False
    flag = True
    for i in range(1, int(num **(1/2))+1):
        if flag:
            flag = False
        else:
            if num % i == 0:
                return False
    return True
Q = int(input())
lis = []
minimam = 10**5
maximam = 1
for _ in range(Q):
    l, r = map(int, input().split())
    lis.append((l, r))
    minimam = min(minimam, l)
    maximam = max(maximam, r)
flag_lis = [0] * (maximam+1)
for num in range(minimam, maximam+1, 2):
    if sosucheck(num) and sosucheck((num+1)/2):
        flag_lis[num] = 1
    else:
        flag_lis[num] = 0
for (l, r) in lis:
    count = 0
    for num in range(l,r+1,2):
        if flag_lis[num]==1:
            count += 1
    print(count)

Fixed submission:
import copy
Q = int(input())
lis = []
maximam = 1
for _ in range(Q):
    l, r = map(int, input().split())
    lis.append((l, r))
    maximam = max(maximam, r)
flag_lis = [1] * (maximam+1)
flag_lis[0] = 0
flag_lis[1] = 0
for i in range(2, int((maximam-1)/2)+1):
    if flag_lis[i] != 0:
        for j in range(2*i,maximam+1,i):
            flag_lis[j] = 0
fin_lis = [0]*(maximam+1)
count = 0
for i in range(3, maximam+1, 2):
    if flag_lis[i] == 1 and flag_lis[int((i+1)/2)] == 1:
        count += 1
    fin_lis[i] = count
for (l, r) in lis:
    if l == 1:
        print(fin_lis[r] - 0)
    else:
        print(fin_lis[r] - fin_lis[l-2])

Buggy python submission (verdict: Time Limit Exceeded):
def sosucheck(num):
    if num == 1:return False
    flag = True
    for i in range(1, int(num **(1/2))+1):
        if flag:
            flag = False
        else:
            if num % i == 0:
                return False
    return True
Q = int(input())
lis = []
for _ in range(Q):
    l, r = map(int, input().split())
    lis.append((l, r))
for (l, r) in lis:
    count = 0
    for num in range(l,r+1,2):
        if sosucheck(num):
            if sosucheck((num+1)/2):
                count += 1
    print(count)

Fixed submission:
import copy
Q = int(input())
lis = []
maximam = 1
for _ in range(Q):
    l, r = map(int, input().split())
    lis.append((l, r))
    maximam = max(maximam, r)
flag_lis = [1] * (maximam+1)
flag_lis[0] = 0
flag_lis[1] = 0
for i in range(2, int((maximam-1)/2)+1):
    if flag_lis[i] != 0:
        for j in range(2*i,maximam+1,i):
            flag_lis[j] = 0
fin_lis = [0]*(maximam+1)
count = 0
for i in range(3, maximam+1, 2):
    if flag_lis[i] == 1 and flag_lis[int((i+1)/2)] == 1:
        count += 1
    fin_lis[i] = count
for (l, r) in lis:
    if l == 1:
        print(fin_lis[r] - 0)
    else:
        print(fin_lis[r] - fin_lis[l-2])