Buggy python submission (verdict: Time Limit Exceeded):
#coding:UTF-8
from collections import defaultdict
d = defaultdict(dict)
def prime_table(n):
    l = [True for _ in range(n + 1)]
    i = 2
    while i * i <= n:
        j = i + i
        while j <= n:
            l[j] = False
            j += i
        i += 1
    table = []
    for i in range(n + 1):
        if l[i] and i >= 2:
            table.append(i)
            d[i] = True
    return table


Q = int(input())
prime_list = prime_table(10 ** 5)
for _ in range(Q):
    count = 0
    l, r = map(int,(input().split()))
    for p in prime_list:
        if l > p:
            continue
        if p > r:
            break
        if p == 2017 or d[((lambda x:(x + 1) // 2)(p))]:
            count += 1
    print(count)


Fixed submission:
#coding:UTF-8

def is_prime(n):
    i = 2
    while i * i <= n:
        if n % i == 0:
            return False
        i += 1
    return True


mx = 10 ** 5
p = [0 for _ in range(mx + 1)]
for i in range(3, mx + 1):
    if i == 2017 or (is_prime(i) and is_prime((i + 1) // 2)):
        p[i] = 1
d = [0 for i in range(mx + 1)]
for i in range(3, mx + 1):
    d[i] += p[i] + d[i - 1]

Q = int(input())
for i in range(Q):
    l, r = map(int, input().split())
    print(d[r] - d[l - 1])

Buggy python submission (verdict: Time Limit Exceeded):
def prime_table(n):
    l = [True for _ in range(n + 1)]
    i = 2
    while i * i <= n:
        j = i + i
        while j <= n:
            l[j] = False
            j += i
        i += 1
    table = [i for i in range(n + 1) if l[i] and i >= 2]
    return table


Q = int(input())
prime_list = prime_table(10 ** 5)
for _ in range(Q):
    count = 0
    l, r = map(int,(input().split()))
    for p in prime_list:
        if l > p:
            continue
        if p > r:
            break
        if p == 2017 or ((lambda x:(x + 1) // 2)(p) in prime_list):
            count += 1
    print(count)


Fixed submission:
#coding:UTF-8

def is_prime(n):
    i = 2
    while i * i <= n:
        if n % i == 0:
            return False
        i += 1
    return True


mx = 10 ** 5
p = [0 for _ in range(mx + 1)]
for i in range(3, mx + 1):
    if i == 2017 or (is_prime(i) and is_prime((i + 1) // 2)):
        p[i] = 1
d = [0 for i in range(mx + 1)]
for i in range(3, mx + 1):
    d[i] += p[i] + d[i - 1]

Q = int(input())
for i in range(Q):
    l, r = map(int, input().split())
    print(d[r] - d[l - 1])

Buggy python submission (verdict: Time Limit Exceeded):
def prime_table(n):
    i = 2
    l = [True for _ in range(n + 1)]
    table = [False for _ in range(n + 1)]
    while i * i <= n:
        j = i + i
        while j <= n:
            l[j] = False
            j += i
        i += 1
    for i in range(n + 1):
        if l[i] and i >= 2:
            table[i] = True
    return table

prime_list = prime_table(10 ** 5)
Q = int(input())

for _ in range(Q):
    count = 0
    l, r = map(int,(input().split()))
    while l <= r:
        if l == 2017 or (prime_list[l] and prime_list[(lambda x:(x + 1) // 2)(l)]):
            count += 1
        l += 2
    print(count)


Fixed submission:
#coding:UTF-8

def is_prime(n):
    i = 2
    while i * i <= n:
        if n % i == 0:
            return False
        i += 1
    return True


mx = 10 ** 5
p = [0 for _ in range(mx + 1)]
for i in range(3, mx + 1):
    if i == 2017 or (is_prime(i) and is_prime((i + 1) // 2)):
        p[i] = 1
d = [0 for i in range(mx + 1)]
for i in range(3, mx + 1):
    d[i] += p[i] + d[i - 1]

Q = int(input())
for i in range(Q):
    l, r = map(int, input().split())
    print(d[r] - d[l - 1])