Buggy python submission (verdict: Time Limit Exceeded):
Q = int(raw_input())
L, R = [], []
for i in range(Q):
	l_r = [int(num) for num in raw_input().split()]
	L.append(l_r[0])
	R.append(l_r[1])

max_num = max(R)
def decide_sosu(max_num):
	sosus_nokori = range(2, max_num + 1)
	sosus = []
	while len(sosus_nokori) > 0:
		min_i = min(sosus_nokori)
		sosus.append(min_i)
		sosus_nokori.remove(min_i)
		for num in range(min_i*min_i, max_num+1, min_i):
			if num in sosus_nokori:
				sosus_nokori.remove(num)
	return sosus

sosus = decide_sosu(max_num+1)

c_s = [0] * (max_num + 1)
for i in range(2, max_num + 1):
	if (i in sosus) & (((i+1) % 2)==0) & ((i+1)/2 in sosus): 
		c_s[i] = c_s[i-1] + 1
	else:
		c_s[i] = c_s[i-1]

for i in range(Q):
	print c_s[R[i]] - c_s[L[i]-1]

Fixed submission:
import math
Q = int(raw_input())
L, R = [], []
for i in range(Q):
	l_r = [int(num) for num in raw_input().split()]
	L.append(l_r[0])
	R.append(l_r[1])

max_num = max(R)
def calc_sosus(max_num):
	IsPrimes = [True] * (max_num + 1)
	IsPrimes[0] = False
	IsPrimes[1] = False
	for min_i in range(2, int(math.sqrt(max_num)) + 1):
		if IsPrimes[min_i]:
			for num in range(min_i*min_i, max_num+1, min_i):
				IsPrimes[num] = False
	return IsPrimes

IsPrimes = calc_sosus(max_num)

c_s = [0] * (max_num + 1)
for i in range(2, max_num + 1):
	if (IsPrimes[i]) & (((i+1) % 2)==0) & (IsPrimes[(i+1)/2]): 
		c_s[i] = c_s[i-1] + 1
	else:
		c_s[i] = c_s[i-1]

for i in range(Q):
	print c_s[R[i]] - c_s[L[i]-1]

Buggy python submission (verdict: Time Limit Exceeded):
Q = int(raw_input())
L, R = [], []
for i in range(Q):
	l_r = [int(num) for num in raw_input().split()]
	L.append(l_r[0])
	R.append(l_r[1])

max_num = max(R)
def decide_sosu(max_num):
	sosus_nokori = range(2, max_num + 1)
	sosus = []
	while len(sosus_nokori) > 0:
		min_i = min(sosus_nokori)
		sosus.append(min_i)
		for num in range(min_i, max_num+1, min_i):
			if num in sosus_nokori:
				sosus_nokori.remove(num)
	return sosus

sosus = decide_sosu(max_num+1)

c_s = [0] * (max_num + 1)
for i in range(2, max_num + 1):
	if (i in sosus) & (((i+1) % 2)==0) & ((i+1)/2 in sosus): 
		c_s[i] = c_s[i-1] + 1
	else:
		c_s[i] = c_s[i-1]

for i in range(Q):
	print c_s[R[i]] - c_s[L[i]-1]

Fixed submission:
import math
Q = int(raw_input())
L, R = [], []
for i in range(Q):
	l_r = [int(num) for num in raw_input().split()]
	L.append(l_r[0])
	R.append(l_r[1])

max_num = max(R)
def calc_sosus(max_num):
	IsPrimes = [True] * (max_num + 1)
	IsPrimes[0] = False
	IsPrimes[1] = False
	for min_i in range(2, int(math.sqrt(max_num)) + 1):
		if IsPrimes[min_i]:
			for num in range(min_i*min_i, max_num+1, min_i):
				IsPrimes[num] = False
	return IsPrimes

IsPrimes = calc_sosus(max_num)

c_s = [0] * (max_num + 1)
for i in range(2, max_num + 1):
	if (IsPrimes[i]) & (((i+1) % 2)==0) & (IsPrimes[(i+1)/2]): 
		c_s[i] = c_s[i-1] + 1
	else:
		c_s[i] = c_s[i-1]

for i in range(Q):
	print c_s[R[i]] - c_s[L[i]-1]

Buggy python submission (verdict: Time Limit Exceeded):
Q = int(raw_input())
L, R = [], []
for i in range(Q):
	l_r = [int(num) for num in raw_input().split()]
	L.append(l_r[0])
	R.append(l_r[1])

max_num = max(R)
def decide_sosu(max_num):
	sosus = []
	for i in range(2, max_num + 1):
		flag = 0
		for num in sosus:
			if i % num == 0:
				flag = 1
				break
		if flag == 0:
			sosus.append(i)
	return sosus

sosus = decide_sosu(max_num+1)

c_s = [0] * (max_num + 1)
for i in range(2, max_num + 1):
	if (i in sosus) & (((i+1) % 2)==0) & ((i+1)/2 in sosus): 
		c_s[i] = c_s[i-1] + 1
	else:
		c_s[i] = c_s[i-1]

for i in range(Q):
	print c_s[R[i]] - c_s[L[i]-1]

Fixed submission:
import math
Q = int(raw_input())
L, R = [], []
for i in range(Q):
	l_r = [int(num) for num in raw_input().split()]
	L.append(l_r[0])
	R.append(l_r[1])

max_num = max(R)
def calc_sosus(max_num):
	IsPrimes = [True] * (max_num + 1)
	IsPrimes[0] = False
	IsPrimes[1] = False
	for min_i in range(2, int(math.sqrt(max_num)) + 1):
		if IsPrimes[min_i]:
			for num in range(min_i*min_i, max_num+1, min_i):
				IsPrimes[num] = False
	return IsPrimes

IsPrimes = calc_sosus(max_num)

c_s = [0] * (max_num + 1)
for i in range(2, max_num + 1):
	if (IsPrimes[i]) & (((i+1) % 2)==0) & (IsPrimes[(i+1)/2]): 
		c_s[i] = c_s[i-1] + 1
	else:
		c_s[i] = c_s[i-1]

for i in range(Q):
	print c_s[R[i]] - c_s[L[i]-1]