Buggy python submission (verdict: Time Limit Exceeded):
import math
Q = int(input())
lr = [list(map(int, input().split())) for i in range(Q)]

max_l = max(lr, key=lambda x:x[1])[1]

a = list(range(2,int(math.ceil(max_l**0.5)+1)))

m = 1
while True:
    b = filter(lambda x: x%a[m-1] != 0,a[m:])
    a = a[:m]
    a.extend(b)
    m += 1
    if m > len(a):
        break

like2017 = [3]
for i in range(5,max_l+1,4):
    for j in a:
        if not ((i % j != 0 or i in a) and ((i+1)/2 % j != 0 or (i+1)/2 in a)):
            break
        if j == a[-1]:
            like2017.append(i)

for i in range(Q):
    print(len(list(filter(lambda x: lr[i][0] <= x <= lr[i][1],like2017))))


Fixed submission:
import math
Q = int(input())
lr = [list(map(int, input().split())) for i in range(Q)]

max_l = max(lr, key=lambda x:x[1])[1]

prime = list(range(2,int(math.ceil(max_l**0.5)+1)))

m = 1
while True:
    b = filter(lambda x: x%prime[m-1] != 0,prime[m:])
    prime = prime[:m]
    prime.extend(b)
    m += 1
    if m > len(prime):
        break

like2017 = {-1:0,1:0}
for i in range(3,max_l+2,2):
    k = 0
    for j in prime:
        if (i%j == 0 or ((i+1)/2)%j == 0) and i != j and (i+1)/2 != j:
            k += 1
            break
    if k == 0:
        like2017[i] = like2017[i-2] + 1
    else:
        like2017[i] = like2017[i-2]


for i in range(Q):
    print(like2017[lr[i][1]]-like2017[lr[i][0]-2])

Buggy python submission (verdict: Time Limit Exceeded):
import math
Q = int(input())
lr = [list(map(int, input().split())) for i in range(Q)]

max_l = max(lr, key=lambda x:x[1])[1]

a = list(range(2,int(math.ceil(max_l**0.5)+1)))
prime = list(range(2,max_l+1))
i = 1
while True:
    b = filter(lambda x: x%a[i-1] != 0,a[i:])
    a = a[:i]
    a.extend(b)
    i += 1
    if i > len(a):
        break

for i in range(len(a)):
    prime = list(filter(lambda x: x%a[i] != 0 or x == a[i],prime))

like2017 = []
for i in prime:
    if (i+1)/2 in prime:
        like2017.append(i)

for i in range(Q):
    print(len(list(filter(lambda x: lr[i][0] <= x <= lr[i][1],like2017))))


Fixed submission:
import math
Q = int(input())
lr = [list(map(int, input().split())) for i in range(Q)]

max_l = max(lr, key=lambda x:x[1])[1]

prime = list(range(2,int(math.ceil(max_l**0.5)+1)))

m = 1
while True:
    b = filter(lambda x: x%prime[m-1] != 0,prime[m:])
    prime = prime[:m]
    prime.extend(b)
    m += 1
    if m > len(prime):
        break

like2017 = {-1:0,1:0}
for i in range(3,max_l+2,2):
    k = 0
    for j in prime:
        if (i%j == 0 or ((i+1)/2)%j == 0) and i != j and (i+1)/2 != j:
            k += 1
            break
    if k == 0:
        like2017[i] = like2017[i-2] + 1
    else:
        like2017[i] = like2017[i-2]


for i in range(Q):
    print(like2017[lr[i][1]]-like2017[lr[i][0]-2])

Buggy python submission (verdict: Time Limit Exceeded):
import math
Q = int(input())
lr = [list(map(int, input().split())) for i in range(Q)]

max_l = max(lr, key=lambda x:x[1])[1]

a = list(range(2,int(math.ceil(max_l**0.5)+1)))
prime = list(range(2,max_l+1))
i = 1
while True:
    b = filter(lambda x: x%a[i-1] != 0,a[i:])
    a = a[:i]
    a.extend(b)
    i += 1
    if i > len(a):
        break

for i in range(len(a)):
    prime = list(filter(lambda x: x%a[i] != 0 or x == a[i],prime))

p = 0
count = {}
for i in range(1,max_l+2,2):
    if (i in prime) and ((i+1)/2 in prime):
        p += 1
    count[i] = p
count[-1] = 0
for i in range(Q):
    print(count[lr[i][1]]-count[lr[i][0]-2])


Fixed submission:
import math
Q = int(input())
lr = [list(map(int, input().split())) for i in range(Q)]

max_l = max(lr, key=lambda x:x[1])[1]

prime = list(range(2,int(math.ceil(max_l**0.5)+1)))

m = 1
while True:
    b = filter(lambda x: x%prime[m-1] != 0,prime[m:])
    prime = prime[:m]
    prime.extend(b)
    m += 1
    if m > len(prime):
        break

like2017 = {-1:0,1:0}
for i in range(3,max_l+2,2):
    k = 0
    for j in prime:
        if (i%j == 0 or ((i+1)/2)%j == 0) and i != j and (i+1)/2 != j:
            k += 1
            break
    if k == 0:
        like2017[i] = like2017[i-2] + 1
    else:
        like2017[i] = like2017[i-2]


for i in range(Q):
    print(like2017[lr[i][1]]-like2017[lr[i][0]-2])

Buggy python submission (verdict: Time Limit Exceeded):
Q = int(input())
lr = [list(map(int, input().split())) for i in range(Q)]

max_l = max(lr, key=lambda x:x[1])[1]

prime =[2]
for i in range(2,max_l+1):
    for j in prime:
        if i % j == 0:
            break
        else:
            if j == prime[-1]:
                prime.append(i)

p = 0
count = {}
for i in range(1,max_l+2,2):
    if (i in prime) and ((i+1)/2 in prime):
        p += 1
    count[i] = p
count[-1] = 0
for i in range(Q):
    print(count[lr[i][1]]-count[lr[i][0]-2])


Fixed submission:
import math
Q = int(input())
lr = [list(map(int, input().split())) for i in range(Q)]

max_l = max(lr, key=lambda x:x[1])[1]

prime = list(range(2,int(math.ceil(max_l**0.5)+1)))

m = 1
while True:
    b = filter(lambda x: x%prime[m-1] != 0,prime[m:])
    prime = prime[:m]
    prime.extend(b)
    m += 1
    if m > len(prime):
        break

like2017 = {-1:0,1:0}
for i in range(3,max_l+2,2):
    k = 0
    for j in prime:
        if (i%j == 0 or ((i+1)/2)%j == 0) and i != j and (i+1)/2 != j:
            k += 1
            break
    if k == 0:
        like2017[i] = like2017[i-2] + 1
    else:
        like2017[i] = like2017[i-2]


for i in range(Q):
    print(like2017[lr[i][1]]-like2017[lr[i][0]-2])

Buggy python submission (verdict: Time Limit Exceeded):
Q = int(input())
lr = [list(map(int, input().split())) for i in range(Q)]

max_l = max(lr, key=lambda x:x[1])[1]

prime =[2]
for i in range(2,max_l+1):
    for j in prime:
        if i % j == 0:
            break
        else:
            if j == prime[-1]:
                prime.append(i)

for i in range(Q):
    count = 0
    for j in range(lr[i][0],lr[i][1]+2,2):
        if (j in prime) and ((j+1)/2 in prime):
            count += 1
    print(count)


Fixed submission:
import math
Q = int(input())
lr = [list(map(int, input().split())) for i in range(Q)]

max_l = max(lr, key=lambda x:x[1])[1]

prime = list(range(2,int(math.ceil(max_l**0.5)+1)))

m = 1
while True:
    b = filter(lambda x: x%prime[m-1] != 0,prime[m:])
    prime = prime[:m]
    prime.extend(b)
    m += 1
    if m > len(prime):
        break

like2017 = {-1:0,1:0}
for i in range(3,max_l+2,2):
    k = 0
    for j in prime:
        if (i%j == 0 or ((i+1)/2)%j == 0) and i != j and (i+1)/2 != j:
            k += 1
            break
    if k == 0:
        like2017[i] = like2017[i-2] + 1
    else:
        like2017[i] = like2017[i-2]


for i in range(Q):
    print(like2017[lr[i][1]]-like2017[lr[i][0]-2])