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

# def primes(x):
#     if x < 2: return []

#     primes = [i for i in range(x)]
#     primes[1] = 0 # 1は素数ではない
#     x_sqrt = math.sqrt(x) # 記憶させていちいち計算しない

#     # エラトステネスのふるい
#     for prime in primes:
#         if prime == 0:
#             continue # 先に処理
#         if prime > x_sqrt: 
#             break
#         for non_prime in range(2 * prime, x, prime):
#             primes[non_prime] = 0

#     return [prime for prime in primes if prime != 0]
def primes_sieve2(limit):
    a = [True] * limit                          # Initialize the primality list
    a[0] = a[1] = False

    for (i, isprime) in enumerate(a):
        if isprime:
            yield i
            for n in range(i*i, limit, i):     # Mark factors non-prime
                a[n] = False
                
q = int(input())
l_list = []
r_list = []
for _ in range(q):
    l, r = list(map(int,input().split()))
    l_list.append(l)
    r_list.append(r)
    
maxr = max(r_list)
p_list = list(primes_sieve2(maxr+1))

sim_list = []
for p in p_list:
    if (p+1)/2 in p_list:
        sim_list.append(p)
        
cumulation = [0 for _ in range(maxr+1)]
for s in sim_list:
    cumulation[s] = 1

for i in range(1,len(cumulation)):
    cumulation[i] += cumulation[i-1]

for i in range(q):
    print(cumulation[r_list[i]]-cumulation[l_list[i]-1])

Fixed submission:
# -*- coding: utf-8 -*-
import math
Q = int(input())

ls = []
rs = []
for n in range(Q):
    l, r = map(int, input().split())
    ls.append(l)
    rs.append(r)
    num = 0

max_r = max(rs)

def eratos(n):
    if n == 1:
        return set()
    primes = [1 for i in range(n+1)]
    primes[0] = 0
    primes[1] = 0
    for i in range(2,math.floor(math.sqrt(n))+1):
        if primes[i] == 1:
            j = i + i
            while j <= n:
                primes[j] = 0
                j = j + i
    return primes


def count_similar(era, n):
    count_similar = [0 for i in range(n+1)]
    for i in range(1,n+1):
        count_similar[i] = count_similar[i-1]
        if era[i] == 1 and era[int((i+1)/2)] == 1:
            count_similar[i] += 1
    return count_similar




era = eratos(max_r)
cnt = count_similar(era, max_r)

for l, r in zip(ls, rs):
    print(cnt[r] - cnt[l-1])

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

def primes(x):
    if x < 2: return []

    primes = [i for i in range(x)]
    primes[1] = 0 # 1は素数ではない
    x_sqrt = math.sqrt(x) # 記憶させていちいち計算しない

    # エラトステネスのふるい
    for prime in primes:
        if prime == 0:
            continue # 先に処理
        if prime > x_sqrt: 
            break
        for non_prime in range(2 * prime, x, prime):
            primes[non_prime] = 0

    return [prime for prime in primes if prime != 0]

q = int(input())
l_list = []
r_list = []
for _ in range(q):
    l, r = list(map(int,input().split()))
    l_list.append(l)
    r_list.append(r)
    
maxr = max(r_list)
p_list = primes(maxr+1)

sim_list = []
for p in p_list:
    if (p+1)/2 in p_list:
        sim_list.append(p)
        
cumulation = [0 for _ in range(maxr+1)]
for s in sim_list:
    cumulation[s] = 1

for i in range(1,len(cumulation)):
    cumulation[i] += cumulation[i-1]

for i in range(q):
    print(cumulation[r_list[i]]-cumulation[l_list[i]-1])

Fixed submission:
# -*- coding: utf-8 -*-
import math
Q = int(input())

ls = []
rs = []
for n in range(Q):
    l, r = map(int, input().split())
    ls.append(l)
    rs.append(r)
    num = 0

max_r = max(rs)

def eratos(n):
    if n == 1:
        return set()
    primes = [1 for i in range(n+1)]
    primes[0] = 0
    primes[1] = 0
    for i in range(2,math.floor(math.sqrt(n))+1):
        if primes[i] == 1:
            j = i + i
            while j <= n:
                primes[j] = 0
                j = j + i
    return primes


def count_similar(era, n):
    count_similar = [0 for i in range(n+1)]
    for i in range(1,n+1):
        count_similar[i] = count_similar[i-1]
        if era[i] == 1 and era[int((i+1)/2)] == 1:
            count_similar[i] += 1
    return count_similar




era = eratos(max_r)
cnt = count_similar(era, max_r)

for l, r in zip(ls, rs):
    print(cnt[r] - cnt[l-1])