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

def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]


q = int(input())
l_r = []
for _ in range(q):
    l, r = map(int, input().split())
    l_r.append((l,r))

can = primes(10**5)
like_2017 = []

for m in can:
    limit = (max(can)//2)
    if m > limit :
        break
    if 2*m-1 in can:
        like_2017.append(2*m-1)

count = 0
can_count = []
for i in range(10**5):
    if i in like_2017:
        count += 1
    else:
        pass
    can_count.append(count)

for pair in l_r:
    l = pair[0]
    r = pair[1]
    ans = can_count[r] - can_count[l-1]
    print(ans)

    


Fixed submission:
import math

def Sieve_of_Eratosthenes(n):
    if n < 2:
        raise ValueError('n is more than 2.')
    prime = []
    limit = math.sqrt(n)
    can = [i for i in range(2, n+1)]
    while True:
        p = can[0]
        if limit <= p:
            return prime + can
        prime.append(p)
        can = [x for x in can if x%p != 0]

q = int(input())
l_r = []
for _ in range(q):
    l, r = map(int, input().split())
    l_r.append((l,r))

can = Sieve_of_Eratosthenes(10**5)
like_2017 = []

for m in can:
    limit = (max(can)//2)
    if m > limit :
        break
    if 2*m-1 in can:
        like_2017.append(2*m-1)

count = 0
can_count = []
for i in range(10**5):
    if i in like_2017:
        count += 1
    else:
        pass
    can_count.append(count)

for pair in l_r:
    l = pair[0]
    r = pair[1]
    ans = can_count[r] - can_count[l-1]
    print(ans)

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

def Sieve_of_Eratosthenes(n):
    if n < 2:
        raise ValueError('n is more than 2.')
    prime = []
    limit = math.sqrt(n)
    can = [i for i in range(2, n+1)]
    while True:
        p = can[0]
        if limit <= p:
            return prime + can
        prime.append(p)
        can = [x for x in can if x%p != 0]

q = int(input())
l_r = []
for _ in range(q):
    l, r = map(int, input().split())
    l_r.append((l,r))

can = Sieve_of_Eratosthenes(10**5)
like_2017 = []

for m in can:
    limit = (max(can)//2)
    if m > limit :
        break
    if 2*m-1 in can:
        like_2017.append(2*m-1)

count = 0
can_count = []
for i in range(10**5):
    if i in like_2017:
        count += 1
    else:
        pass
    can_count.append(count)

for pair in l_r:
    l = pair[0]
    r = pair[1]
    ans = can_count[r] - can_count[l-1]
    print(ans)

    


Fixed submission:
import math

def Sieve_of_Eratosthenes(n):
    if n < 2:
        raise ValueError('n is more than 2.')
    prime = []
    limit = math.sqrt(n)
    can = [i for i in range(2, n+1)]
    while True:
        p = can[0]
        if limit <= p:
            return prime + can
        prime.append(p)
        can = [x for x in can if x%p != 0]

q = int(input())
l_r = []
for _ in range(q):
    l, r = map(int, input().split())
    l_r.append((l,r))

can = Sieve_of_Eratosthenes(10**5)
like_2017 = []

for m in can:
    limit = (max(can)//2)
    if m > limit :
        break
    if 2*m-1 in can:
        like_2017.append(2*m-1)

count = 0
can_count = []
for i in range(10**5):
    if i in like_2017:
        count += 1
    else:
        pass
    can_count.append(count)

for pair in l_r:
    l = pair[0]
    r = pair[1]
    ans = can_count[r] - can_count[l-1]
    print(ans)