Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import bisect, bisect_left
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def get_sieve_of_eratosthenes(n):
	prime = [2]
	limit = int(n**0.5)
	data = [i + 1 for i in range(2, n, 2)]
	append = prime.append
	while True:
		p = data[0]
		if p > limit:
			return prime + data
		append(p)
		data = [e for e in data if e % p != 0]

def main():
	Q = INT()
	A = [[] for _ in range(Q)]
	
	qmax = 0
	for i in range(Q):
		l, r = MAP()
		qmax = max(qmax, r)
		A[i] = [l, r]
	
	primes = get_sieve_of_eratosthenes(qmax)
	primes = [p for p in primes if primes.count((p+1)//2)]
	# print(primes)

	for a in A:
		l, r = a
		print(bisect(primes, r)-bisect_left(primes, l))

if __name__ == '__main__':
	main()

Fixed submission:
import sys
from bisect import bisect, bisect_left

sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def get_sieve_of_eratosthenes(n):
	prime = [2]
	limit = int(n**0.5)
	data = [i + 1 for i in range(2, n, 2)]
	append = prime.append
	while True:
		p = data[0]
		if p > limit:
			return prime + data
		append(p)
		data = [e for e in data if e % p != 0]

def main():
	primes = get_sieve_of_eratosthenes(10**5)

	prime_dict = dict(zip(primes, [1]*len(primes)))
	prime_2017 = []
	append = prime_2017.append
	for k in prime_dict.keys():
		if int((k+1)/2) in prime_dict:
			append(k)
	
	prime_2017.sort()
	# print(prime_2017)

	Q = INT()
	for _ in range(Q):
		l, r = MAP()
		print(bisect(prime_2017, r)-bisect_left(prime_2017, l))

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import bisect, bisect_left
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def get_sieve_of_eratosthenes(n):
	prime = [2]
	limit = int(n**0.5)
	data = [i + 1 for i in range(2, n, 2)]
	append = prime.append
	while True:
		p = data[0]
		if p > limit:
			return prime + data
		append(p)
		data = [e for e in data if e % p != 0]

def main():
	import time
	primes = get_sieve_of_eratosthenes(10**5)
	primes = [p for p in primes if primes.count((p+1)//2)]
	# print(primes)
	Q = INT()
	for _ in range(Q):
		l, r = MAP()
		print(bisect(primes, r)-bisect_left(primes, l))

if __name__ == '__main__':
	main()

Fixed submission:
import sys
from bisect import bisect, bisect_left

sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def get_sieve_of_eratosthenes(n):
	prime = [2]
	limit = int(n**0.5)
	data = [i + 1 for i in range(2, n, 2)]
	append = prime.append
	while True:
		p = data[0]
		if p > limit:
			return prime + data
		append(p)
		data = [e for e in data if e % p != 0]

def main():
	primes = get_sieve_of_eratosthenes(10**5)

	prime_dict = dict(zip(primes, [1]*len(primes)))
	prime_2017 = []
	append = prime_2017.append
	for k in prime_dict.keys():
		if int((k+1)/2) in prime_dict:
			append(k)
	
	prime_2017.sort()
	# print(prime_2017)

	Q = INT()
	for _ in range(Q):
		l, r = MAP()
		print(bisect(prime_2017, r)-bisect_left(prime_2017, l))

if __name__ == '__main__':
	main()