Buggy python submission (verdict: Time Limit Exceeded):
import math
import bisect
Q=int(input())

querys = [list(map(int,input().split())) for x in range(Q)]

def siever(x):
  nums = [a for a in range(2,x+1)]
  primes = []
  while nums[0] <= int(math.sqrt(x))+1:
    tmp = nums.pop(0)
    primes.append(tmp)
    for num in nums:
      if num%tmp == 0:
        nums.remove(num)
        continue
  
  return (primes+nums)

L = 0
for query in querys:
  L = max(L,query[1])
primes = siever(L)
L2017= []
for prime in primes:
  if (prime+1)//2 in primes:
    L2017.append(prime)
result = [0]*(max(L2017)+1)
for l in L2017:
  result[l] = 1
for i in range(1,len(result)):
  result[i] += result[i-1]
for query in querys:
  if query[0] == query[1] and query[0] in L2017:
    print(1)
  else:
    if len(result) <= query[0]:
      print(0)
    elif len(result) <= query[1]:
      print(result[-1]-result[query[0]-1])
    else:
      print(result[query[1]]-result[query[0]-1])

Fixed submission:
import math
Q=int(input())
querys = [list(map(int,input().split())) for x in range(Q)]
large = max(q[1] for q in querys)
like2017 = [0]*(large+1)

#siever
cands = [False if (5 < i and (i%2)*(i%3)*(i%5) == 0) or (i < 2) or (i == 4) else True for i in range(large+1)]
if 5 <= large:
  for i in range(6,int(math.sqrt(large)+1)):
    if cands[i]:
      for j in range(i*i,large+1,i):
        if cands[j]:
          cands[j] = False

#find 2017-like-number          
for x in range(large+1):
  if cands[x] and cands[(x+1)//2]:
    like2017[x] = 1
for y in range(1,large+1):
  like2017[y] += like2017[y-1]

for q in querys:
  print(like2017[q[1]]-like2017[q[0]-1])

Buggy python submission (verdict: Time Limit Exceeded):
import math
import bisect
Q=int(input())

querys = [list(map(int,input().split())) for x in range(Q)]

def siever(x):
  nums = [a for a in range(2,x+1)]
  primes = []
  while nums[0] <= int(math.sqrt(x))+1:
    tmp = nums.pop(0)
    primes.append(tmp)
    for num in nums:
      if num%tmp == 0:
        nums.remove(num)
        continue
  
  return (primes+nums)

L = 0
for query in querys:
  L = max(L,query[1])
primes = siever(L)
L2017= []
for prime in primes:
  if (prime+1)//2 in primes:
    L2017.append(prime)
result = [0]*(max(L2017)+1)
for l in L2017:
  result[l] = 1
for i in range(1,len(result)):
  result[i] += result[i-1]
for query in querys:
  if query[0] == query[1] and query[0] in L2017:
    print(1)
  else:
    if len(result) <= query[0]:
      print(0)
    elif len(result) <= query[1]:
      print(result[-1]-result[query[0]-1])
    else:
      print(result[query[1]]-result[query[0]-1])

Fixed submission:
import math
Q=int(input())
querys = [list(map(int,input().split())) for x in range(Q)]
large = max(q[1] for q in querys)
like2017 = [0]*(large+1)

#siever
cands = [False if (5 < i and (i%2)*(i%3)*(i%5) == 0) or (i < 2) or (i == 4) else True for i in range(large+1)]
if 5 <= large:
  for i in range(6,int(math.sqrt(large)+1)):
    if cands[i]:
      for j in range(i*i,large+1,i):
        if cands[j]:
          cands[j] = False

#find 2017-like-number          
for x in range(large+1):
  if cands[x] and cands[(x+1)//2]:
    like2017[x] = 1
for y in range(1,large+1):
  like2017[y] += like2017[y-1]

for q in querys:
  print(like2017[q[1]]-like2017[q[0]-1])

Buggy python submission (verdict: Time Limit Exceeded):
import math
import bisect
Q=int(input())

querys = [list(map(int,input().split())) for x in range(Q)]

def siever(x):
  nums = [a for a in range(2,x+1)]
  primes = []
  while nums[0] <= int(math.sqrt(x))+1:
    tmp = nums.pop(0)
    primes.append(tmp)
    for num in nums:
      if num%tmp == 0:
        nums.remove(num)
        continue
  
  return (primes+nums)

L = 0
for query in querys:
  L = max(L,query[1])
primes = siever(L)
L2017= []
for prime in primes:
  if (prime+1)//2 in primes:
    L2017.append(prime)
for query in querys:
  if query[0] == query[1]:
    if query[0] in L2017:
      print(1)
    else:
      print(0)
  else: 
    print(bisect.bisect_right(L2017,query[1]) - bisect.bisect_left(L2017,query[0]))

Fixed submission:
import math
Q=int(input())
querys = [list(map(int,input().split())) for x in range(Q)]
large = max(q[1] for q in querys)
like2017 = [0]*(large+1)

#siever
cands = [False if (5 < i and (i%2)*(i%3)*(i%5) == 0) or (i < 2) or (i == 4) else True for i in range(large+1)]
if 5 <= large:
  for i in range(6,int(math.sqrt(large)+1)):
    if cands[i]:
      for j in range(i*i,large+1,i):
        if cands[j]:
          cands[j] = False

#find 2017-like-number          
for x in range(large+1):
  if cands[x] and cands[(x+1)//2]:
    like2017[x] = 1
for y in range(1,large+1):
  like2017[y] += like2017[y-1]

for q in querys:
  print(like2017[q[1]]-like2017[q[0]-1])

Buggy python submission (verdict: Time Limit Exceeded):
Q = int(input())
prime = [2]
NUM2017 = []
def setPrime(TOP):
    for x in range(3,TOP+1):
        for y in range(len(prime)):
            if x%prime[y] == 0:
                break
            if y == len(prime)-1:
                prime.append(x)
                if int((x+1)/2) in prime:
                    NUM2017.append(x)
l = [0]*Q
r = [0]*Q
for i in range(Q):
    l[i],r[i] = list(map(int,input().split()))

setPrime(max(r))

for n in range(Q):
    left = 0
    right = 0
    for m in range(len(NUM2017)):
        if NUM2017[m] < l[n]:
            left +=  1
            right += 1
        elif l[n] <= NUM2017[m] and NUM2017[m] <= r[n]:
            right += 1
        else:
            break
    print(right-left)

Fixed submission:
import math
Q=int(input())
querys = [list(map(int,input().split())) for x in range(Q)]
large = max(q[1] for q in querys)
like2017 = [0]*(large+1)

#siever
cands = [False if (5 < i and (i%2)*(i%3)*(i%5) == 0) or (i < 2) or (i == 4) else True for i in range(large+1)]
if 5 <= large:
  for i in range(6,int(math.sqrt(large)+1)):
    if cands[i]:
      for j in range(i*i,large+1,i):
        if cands[j]:
          cands[j] = False

#find 2017-like-number          
for x in range(large+1):
  if cands[x] and cands[(x+1)//2]:
    like2017[x] = 1
for y in range(1,large+1):
  like2017[y] += like2017[y-1]

for q in querys:
  print(like2017[q[1]]-like2017[q[0]-1])

Buggy python submission (verdict: Time Limit Exceeded):
Q = int(input())
prime = [2]
def setPrime(TOP):
    for x in range(3,TOP+1):
        for y in range(len(prime)):
            if x%prime[y] == 0:
                break
            if y == len(prime)-1:
                prime.append(x)
l = [0]*Q
r = [0]*Q
for i in range(Q):
    l[i],r[i] = list(map(int,input().split()))

setPrime(max(r))

result = [0]*max(r)
for x in range(len(prime)):
    if int((prime[x]+1)/2) in prime:
        for y in range(prime[x]-1,max(r)):
            result[y] += 1

for n in range(Q):
    if l[n] > 1:
        print(result[r[n]-1]-result[l[n]-2])
    else:
        print(result[r[n]-1])

Fixed submission:
import math
Q=int(input())
querys = [list(map(int,input().split())) for x in range(Q)]
large = max(q[1] for q in querys)
like2017 = [0]*(large+1)

#siever
cands = [False if (5 < i and (i%2)*(i%3)*(i%5) == 0) or (i < 2) or (i == 4) else True for i in range(large+1)]
if 5 <= large:
  for i in range(6,int(math.sqrt(large)+1)):
    if cands[i]:
      for j in range(i*i,large+1,i):
        if cands[j]:
          cands[j] = False

#find 2017-like-number          
for x in range(large+1):
  if cands[x] and cands[(x+1)//2]:
    like2017[x] = 1
for y in range(1,large+1):
  like2017[y] += like2017[y-1]

for q in querys:
  print(like2017[q[1]]-like2017[q[0]-1])

Buggy python submission (verdict: Time Limit Exceeded):
Q = int(input())
prime = [2]

def setPrime(TOP):
    for x in range(3,TOP+1):
        for y in range(len(prime)):
            if x%prime[y] == 0:
                break
            if y == len(prime)-1:
                prime.append(x)
l = [0]*Q
r = [0]*Q
for i in range(Q):
    l[i],r[i] = list(map(int,input().split()))

setPrime(max(r))

for n in range(Q):
    result = 0
    for m in range(l[n],r[n]+1):
        if m in prime and int((m+1)/2) in prime:
            result += 1
    print(result)
            

Fixed submission:
import math
Q=int(input())
querys = [list(map(int,input().split())) for x in range(Q)]
large = max(q[1] for q in querys)
like2017 = [0]*(large+1)

#siever
cands = [False if (5 < i and (i%2)*(i%3)*(i%5) == 0) or (i < 2) or (i == 4) else True for i in range(large+1)]
if 5 <= large:
  for i in range(6,int(math.sqrt(large)+1)):
    if cands[i]:
      for j in range(i*i,large+1,i):
        if cands[j]:
          cands[j] = False

#find 2017-like-number          
for x in range(large+1):
  if cands[x] and cands[(x+1)//2]:
    like2017[x] = 1
for y in range(1,large+1):
  like2017[y] += like2017[y-1]

for q in querys:
  print(like2017[q[1]]-like2017[q[0]-1])