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


def solve(l, r, primeset):
    res = 0
    for i in range(l, r + 1, 2):
        if i in primeset and (i + 1) // 2 in primeset:
            res += 1
    return res


input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    primelist = [2]
    A = 10 ** 5 + 10
    for L in range(3, A, 2):
        if all(L % L2 != 0 for L2 in primelist):
            primelist.append(L)
    primeset = set(primelist)
    # print(primelist[:20])
    Q = int(input())
    for i in range(Q):
        l, r = map(int, input().split())
        print(solve(l, r, primeset))


if __name__ == "__main__":
    main()


Fixed submission:
MAX = 10 ** 5 + 1
f = [True] * (MAX)
c = [0] * (MAX + 1)
for i in range(2, MAX):
    if f[i]:
        for j in range(i + i, MAX, i):
            f[j] = False
for i in range(3, MAX, 2):
    if f[i] and f[(i + 1) // 2]:
        c[i] += 1
for i in range(3, MAX):
    c[i] += c[i - 1]

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

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


def isprime(x):
    # 素数判定
    if x == 2:
        return True
    if x < 2 or x % 2 == 0:
        return False
    i = 3
    while i <= x ** 0.5:
        if x % i == 0:
            return False
        i += 2
    return True


def solve(l, r, primelist, primelist2):
    res = 0
    for i in range(l, r + 1, 2):
        if primelist[i] and primelist2[i]:
            res += 1
    return res


input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    primelist = [False] * (10 ** 5 + 10)
    primelist2 = [False] * (10 ** 5 + 10)
    primelist[2] = True
    for i in range(1, 10 ** 5 + 1, 2):
        if isprime(i):
            primelist[i] = True
    for i in range(1, 10 ** 5 + 1, 2):
        if primelist[(i + 1) // 2]:
            primelist2[i] = True
    # print(primelist[:20])
    Q = int(input())
    for i in range(Q):
        l, r = map(int, input().split())
        print(solve(l, r, primelist, primelist2))


if __name__ == "__main__":
    main()


Fixed submission:
MAX = 10 ** 5 + 1
f = [True] * (MAX)
c = [0] * (MAX + 1)
for i in range(2, MAX):
    if f[i]:
        for j in range(i + i, MAX, i):
            f[j] = False
for i in range(3, MAX, 2):
    if f[i] and f[(i + 1) // 2]:
        c[i] += 1
for i in range(3, MAX):
    c[i] += c[i - 1]

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

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


def isprime(x):
    # 素数判定
    if x == 2:
        return True
    if x < 2 or x % 2 == 0:
        return False
    i = 3
    while i <= x ** 0.5:
        if x % i == 0:
            return False
        i += 2
    return True


def solve(l, r, primelist, primelist2):
    res = 0
    for i in range(l, r + 1, 2):
        if primelist[i] and primelist2[i]:
            res += 1
    return res


input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    primelist = [False] * (10 ** 5 + 10)
    primelist2 = [False] * (10 ** 5 + 10)
    primelist[2] = True
    for i in range(1, 10 ** 5 + 1, 2):
        if isprime(i):
            primelist[i] = True
        if isprime((i + 1) // 2):
            primelist2[i] = True
    # print(primelist[:20])
    Q = int(input())
    for i in range(Q):
        l, r = map(int, input().split())
        print(solve(l, r, primelist, primelist2))


if __name__ == "__main__":
    main()


Fixed submission:
MAX = 10 ** 5 + 1
f = [True] * (MAX)
c = [0] * (MAX + 1)
for i in range(2, MAX):
    if f[i]:
        for j in range(i + i, MAX, i):
            f[j] = False
for i in range(3, MAX, 2):
    if f[i] and f[(i + 1) // 2]:
        c[i] += 1
for i in range(3, MAX):
    c[i] += c[i - 1]

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

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


def isprime(x):
    # 素数判定
    if x == 2:
        return True
    if x < 2 or x % 2 == 0:
        return False
    i = 3
    while i <= x ** 0.5:
        if x % i == 0:
            return False
        i += 2
    return True


def solve(l, r, primelist, primelist2):
    res = 0
    for i in range(l, r + 1, 2):
        if primelist[i] and primelist2[i]:
            res += 1
    return res


input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    primelist = [False] * (10 ** 5 + 10)
    primelist2 = [False] * (10 ** 5 + 10)
    primelist[2] = True
    for i in range(1, 10 ** 5 + 1, 2):
        if isprime(i):
            primelist[i] = True
        if isprime((i + 1) // 2):
            primelist2[i] = True
    # print(primelist[:20])
    Q = int(input())
    for i in range(Q):
        l, r = map(int, input().split())
        print(solve(l, r, primelist, primelist2))


if __name__ == "__main__":
    main()


Fixed submission:
MAX = 10 ** 5 + 1
f = [True] * (MAX)
c = [0] * (MAX + 1)
for i in range(2, MAX):
    if f[i]:
        for j in range(i + i, MAX, i):
            f[j] = False
for i in range(3, MAX, 2):
    if f[i] and f[(i + 1) // 2]:
        c[i] += 1
for i in range(3, MAX):
    c[i] += c[i - 1]

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

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


def isprime(x):
    # 素数判定
    if x == 2:
        return True
    if x < 2 or x % 2 == 0:
        return False
    i = 3
    while i <= x ** 0.5:
        if x % i == 0:
            return False
        i += 2
    return True


def solve(l, r, primelist):
    res = 0
    for i in range(l, r + 1, 2):
        if primelist[i] and primelist[(i + 1) // 2]:
            res += 1
    return res


input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    primelist = [False] * (10 ** 5 + 10)
    primelist[2] = True
    for i in range(1, 10 ** 5 + 1, 2):
        if isprime(i):
            primelist[i] = True
    # print(primelist[:20])
    Q = int(input())
    for i in range(Q):
        l, r = map(int, input().split())
        print(solve(l, r, primelist))


if __name__ == "__main__":
    main()


Fixed submission:
MAX = 10 ** 5 + 1
f = [True] * (MAX)
c = [0] * (MAX + 1)
for i in range(2, MAX):
    if f[i]:
        for j in range(i + i, MAX, i):
            f[j] = False
for i in range(3, MAX, 2):
    if f[i] and f[(i + 1) // 2]:
        c[i] += 1
for i in range(3, MAX):
    c[i] += c[i - 1]

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

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


def isprime(x):
    # 素数判定
    if x == 2:
        return True
    if x < 2 or x % 2 == 0:
        return False
    i = 3
    while i <= x ** 0.5:
        if x % i == 0:
            return False
        i += 2
    return True


def solve(l, r, primelist):
    res = 0
    for i in range(l, r + 1, 2):
        if primelist[i] and primelist[(i + 1) // 2]:
            res += 1
    return res


input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    primelist = [False] * (10 ** 5 + 10)
    primelist[2] = True
    for i in range(1, 10 ** 5 + 1, 2):
        if isprime(i):
            primelist[i] = True
    # print(primelist[:20])
    Q = int(input())
    for i in range(Q):
        l, r = map(int, input().split())
        print(solve(l, r, primelist))


if __name__ == "__main__":
    main()


Fixed submission:
MAX = 10 ** 5 + 1
f = [True] * (MAX)
c = [0] * (MAX + 1)
for i in range(2, MAX):
    if f[i]:
        for j in range(i + i, MAX, i):
            f[j] = False
for i in range(3, MAX, 2):
    if f[i] and f[(i + 1) // 2]:
        c[i] += 1
for i in range(3, MAX):
    c[i] += c[i - 1]

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