Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import sys


def make_primes(n):
    primes = [2]
    for i in range(3, n, 2):
        if is_prime(i, primes):
            primes.append(i)
    return primes


def is_prime(n, primes):
    for prime in primes:
        if n % prime == 0:
            return False
        if n // 2 < prime:
            break
    return True


def make_like2017():
    like2017 = [0]
    for idx in range(1, len(primes)):
        if is_prime((primes[idx] + 1) // 2, primes):
            like2017.append(like2017[idx - 1] + 1)
        else:
            like2017.append(like2017[idx - 1])
    return like2017


def sol(l, r):
    if l == r:
        if is_prime(l, primes) and is_prime((l + 1) // 2, primes):
            return 1
        else:
            return 0

    idx_l = bisect.bisect_left(primes, l)
    idx_r = bisect.bisect_left(primes, r)

    if idx_l == idx_r:
        return 0
    else:
        idx_l = max(0, idx_l - 1)
        idx_r = min(len(like2017s) - 1, idx_r)
        return like2017s[idx_r] - like2017s[idx_l]


N_MAX = 10 ** 5 + 1

Q = int(sys.stdin.readline())
primes = make_primes(N_MAX)
like2017s = make_like2017()

for _ in range(Q):
    l, r = map(int, sys.stdin.readline().split())
    print(sol(l, r))


Fixed submission:
import math
import bisect
  
def is_prime(n):
    if n == 1: return False
    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False
    return True

def get_like_2017(N):
  nums = set([i for i in range(2,N+1)])
  for i in range(2, int(math.sqrt(N))+1):
    for j in range(2,int(N//i)+1):
      if i*j in nums: nums.remove(i*j)
  
  nums_like_2017 = set([])
  for num in nums:
    if is_prime(int((num+1)/2)) is True:  nums_like_2017.add(num)
  return nums_like_2017

nums_like_2017 = sorted(list(get_like_2017(10**5)))

Q = int(input())
for _ in range(Q):
  l,r = map(int, input().split())
  idx_l = bisect.bisect_left(nums_like_2017, l)
  idx_r = bisect.bisect_right(nums_like_2017, r)
 
  print(idx_r-idx_l)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import sys


def make_primes(n):
    primes = [2]
    for i in range(3, n, 2):
        if all(i % prime != 0 for prime in primes):
            primes.append(i)
    return primes


def make_like2017():
    like2017 = [0]
    for idx in range(1, len(primes)):
        if (primes[idx] + 1) // 2 in primes:
            like2017.append(like2017[idx - 1] + 1)
        else:
            like2017.append(like2017[idx - 1])
    return like2017


def sol(l, r):
    if l == r:
        if l in primes and (l + 1) // 2 in primes:
            return 1
        else:
            return 0

    idx_l = bisect.bisect_left(primes, l)
    idx_r = bisect.bisect_left(primes, r)

    if idx_l == idx_r:
        return 0
    else:
        idx_l = max(0, idx_l - 1)
        idx_r = min(len(like2017s) - 1, idx_r)
        return like2017s[idx_r] - like2017s[idx_l]


N_MAX = 10 ** 5 + 1

Q = int(sys.stdin.readline())
primes = make_primes(N_MAX)
like2017s = make_like2017()

for _ in range(Q):
    l, r = map(int, sys.stdin.readline().split())
    print(sol(l, r))


Fixed submission:
import math
import bisect
  
def is_prime(n):
    if n == 1: return False
    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False
    return True

def get_like_2017(N):
  nums = set([i for i in range(2,N+1)])
  for i in range(2, int(math.sqrt(N))+1):
    for j in range(2,int(N//i)+1):
      if i*j in nums: nums.remove(i*j)
  
  nums_like_2017 = set([])
  for num in nums:
    if is_prime(int((num+1)/2)) is True:  nums_like_2017.add(num)
  return nums_like_2017

nums_like_2017 = sorted(list(get_like_2017(10**5)))

Q = int(input())
for _ in range(Q):
  l,r = map(int, input().split())
  idx_l = bisect.bisect_left(nums_like_2017, l)
  idx_r = bisect.bisect_right(nums_like_2017, r)
 
  print(idx_r-idx_l)

Buggy python submission (verdict: Time Limit Exceeded):
import math
import bisect

def is_like_2017(num):
  if all([is_prime(num), is_prime(int((num+1)/2))]): return True
  else: return False
  
def is_prime(n):
    if n == 1: return False
    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False
    return True

def get_prime_nums(N):
  nums = set([i for i in range(2,N+1)])
  for i in range(2, int(math.sqrt(N))+1):
    for j in range(2,int(N//i)+1):
      if i*j in nums: nums.remove(i*j)
  return nums

prime_nums = list(get_prime_nums(10**5))

Q = int(input())
for _ in range(Q):
  l,r = map(int, input().split())
  idx_l = bisect.bisect_left(prime_nums, l)
  idx_r = bisect.bisect_right(prime_nums, r)
 
  ans = 0
  for i in range(idx_l, idx_r):
    if is_like_2017(prime_nums[i]) is True:ans += 1
  print(ans)
    

Fixed submission:
import math
import bisect
  
def is_prime(n):
    if n == 1: return False
    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False
    return True

def get_like_2017(N):
  nums = set([i for i in range(2,N+1)])
  for i in range(2, int(math.sqrt(N))+1):
    for j in range(2,int(N//i)+1):
      if i*j in nums: nums.remove(i*j)
  
  nums_like_2017 = set([])
  for num in nums:
    if is_prime(int((num+1)/2)) is True:  nums_like_2017.add(num)
  return nums_like_2017

nums_like_2017 = sorted(list(get_like_2017(10**5)))

Q = int(input())
for _ in range(Q):
  l,r = map(int, input().split())
  idx_l = bisect.bisect_left(nums_like_2017, l)
  idx_r = bisect.bisect_right(nums_like_2017, r)
 
  print(idx_r-idx_l)

Buggy python submission (verdict: Time Limit Exceeded):
import math

def is_like_2017(num):
  if all([is_prime(num), is_prime(int((num+1)/2))]): return True
  else: return False
  
def is_prime(n):
    if n == 1: return False
    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False
    return True

Q = int(input())
for _ in range(Q):
  l,r = map(int, input().split())
  ans = 0
  for i in range(l,r+1):
    if is_like_2017(i): ans += 1
  print(ans)
    

Fixed submission:
import math
import bisect
  
def is_prime(n):
    if n == 1: return False
    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False
    return True

def get_like_2017(N):
  nums = set([i for i in range(2,N+1)])
  for i in range(2, int(math.sqrt(N))+1):
    for j in range(2,int(N//i)+1):
      if i*j in nums: nums.remove(i*j)
  
  nums_like_2017 = set([])
  for num in nums:
    if is_prime(int((num+1)/2)) is True:  nums_like_2017.add(num)
  return nums_like_2017

nums_like_2017 = sorted(list(get_like_2017(10**5)))

Q = int(input())
for _ in range(Q):
  l,r = map(int, input().split())
  idx_l = bisect.bisect_left(nums_like_2017, l)
  idx_r = bisect.bisect_right(nums_like_2017, r)
 
  print(idx_r-idx_l)