Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python

import math
import sys

def is_prime(n):
    if n == 1:
        return False

    for i in xrange(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

def main(argv):
    primes = [False]
    for i in xrange(1, 100000 + 1):
        primes.append(is_prime(i))

    results = [0]
    for i in xrange(1, 100000 + 1):
        if i % 2 == 0:
            results.append(0)
            continue

        if primes[i] and primes[(i + 1) / 2]:
            results.append(1)
        else:
            results.append(0)

    line = sys.stdin.readline()
    while line:
        Q = int(line.rstrip("\n"))
        for i in xrange(Q):
            l, r = map(
              lambda i: int(i), sys.stdin.readline().rstrip("\n").split(" "))

            print sum(results[l:r+1])

        line = sys.stdin.readline()

if __name__ == "__main__":
    main(sys.argv)

Fixed submission:
#!/usr/bin/python

import math
import sys

def is_prime(n):
    if n == 1:
        return False

    for i in xrange(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

def main(argv):
    primes = [False]
    for i in xrange(1, 100000 + 1):
        primes.append(is_prime(i))

    counter = 0
    results = [0]
    for i in xrange(1, 100000 + 1):
        if i % 2 == 0:
            results.append(counter)
            continue

        if primes[i] and primes[(i + 1) / 2]:
            counter += 1
        results.append(counter)

    line = sys.stdin.readline()
    while line:
        Q = int(line.rstrip("\n"))
        for i in xrange(Q):
            l, r = map(
              lambda i: int(i), sys.stdin.readline().rstrip("\n").split(" "))

            print results[r] - results[l - 1]

        line = sys.stdin.readline()

if __name__ == "__main__":
    main(sys.argv)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python

import math
import sys

def is_prime(n):
    if n == 1:
        return False

    for i in xrange(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

def main(argv):
    primes = {}
    for i in xrange(2, 10000):
        if is_prime(i):
            primes[i] = True

    line = sys.stdin.readline()
    while line:
        Q = int(line.rstrip("\n"))
        for i in xrange(Q):
            l, r = map(
              lambda i: int(i), sys.stdin.readline().rstrip("\n").split(" "))

            counter = 0
            while l <= r:
                if l in primes and (l + 1) / 2 in primes:
                    counter += 1
                l += 2
            print counter

        line = sys.stdin.readline()

if __name__ == "__main__":
    main(sys.argv)

Fixed submission:
#!/usr/bin/python

import math
import sys

def is_prime(n):
    if n == 1:
        return False

    for i in xrange(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

def main(argv):
    primes = [False]
    for i in xrange(1, 100000 + 1):
        primes.append(is_prime(i))

    counter = 0
    results = [0]
    for i in xrange(1, 100000 + 1):
        if i % 2 == 0:
            results.append(counter)
            continue

        if primes[i] and primes[(i + 1) / 2]:
            counter += 1
        results.append(counter)

    line = sys.stdin.readline()
    while line:
        Q = int(line.rstrip("\n"))
        for i in xrange(Q):
            l, r = map(
              lambda i: int(i), sys.stdin.readline().rstrip("\n").split(" "))

            print results[r] - results[l - 1]

        line = sys.stdin.readline()

if __name__ == "__main__":
    main(sys.argv)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python

import math
import sys

def is_prime(n):
    if n == 1:
        return False

    for i in xrange(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

def main(argv):
    line = sys.stdin.readline()
    while line:
        Q = int(line.rstrip("\n"))
        for i in xrange(Q):
            l, r = map(
              lambda i: int(i), sys.stdin.readline().rstrip("\n").split(" "))

            counter = 0
            while l <= r:
                if is_prime(l) and is_prime((l + 1) / 2):
                    counter += 1
                l += 2
            print counter

        line = sys.stdin.readline()

if __name__ == "__main__":
    main(sys.argv)

Fixed submission:
#!/usr/bin/python

import math
import sys

def is_prime(n):
    if n == 1:
        return False

    for i in xrange(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

def main(argv):
    primes = [False]
    for i in xrange(1, 100000 + 1):
        primes.append(is_prime(i))

    counter = 0
    results = [0]
    for i in xrange(1, 100000 + 1):
        if i % 2 == 0:
            results.append(counter)
            continue

        if primes[i] and primes[(i + 1) / 2]:
            counter += 1
        results.append(counter)

    line = sys.stdin.readline()
    while line:
        Q = int(line.rstrip("\n"))
        for i in xrange(Q):
            l, r = map(
              lambda i: int(i), sys.stdin.readline().rstrip("\n").split(" "))

            print results[r] - results[l - 1]

        line = sys.stdin.readline()

if __name__ == "__main__":
    main(sys.argv)