import math



def is_prime(n):

    if n==1 :

        return False



    for k in range(2,int(math.sqrt(n))+1):

        if n%k==0:

            return False



    return True



count = [0]

buf = 0

for i in range(1, 10**5 + 2):

    if i % 2 == 0:

        count.append(buf)

        continue



    hantei1 = is_prime(i)

    hantei2 = is_prime((i+1)//2)



    if hantei1 and hantei2:

        buf += 1



    count.append(buf)



q = int(eval(input()))



for i in range(q):

    l,r = list(map(int,input().split()))

    print((count[r]-count[l-1]))
def prime(p):

    primeFlag = True

    if p == 1:

        primeFlag = False

    for i in range(2,(int(p**0.5)+1)):

        if p%i==0:

            primeFlag = False

            break

    if primeFlag:

        return True

    else:

        return False

    

Q = int(eval(input()))



memo = [0]*100001



for i in range(2,100001):

    first = prime(i)

    second = prime((i+1)//2)

    if first and second:

        memo[i] = 1



for i in range(1,100001):

    memo[i] = memo[i-1]+memo[i]



for i in range(Q):

    l,r = list(map(int,input().split()))

    print((memo[r]-memo[l-1]))
import math


def is_prime(n):
    if n == 1:
        return False
    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False
    return True


count = [0]
buf = 0
for i in range(1, 10**5 + 2):
    if i % 2 == 0:
        count.append(buf)
        continue
    hantei1 = is_prime(i)
    hantei2 = is_prime((i + 1) // 2)
    if hantei1 and hantei2:
        buf += 1
    count.append(buf)
q = int(eval(input()))
for i in range(q):
    l, r = list(map(int, input().split()))
    print((count[r] - count[l - 1]))

def prime(p):
    primeFlag = True
    if p == 1:
        primeFlag = False
    for i in range(2, (int(p**0.5) + 1)):
        if p % i == 0:
            primeFlag = False
            break
    if primeFlag:
        return True
    else:
        return False


Q = int(eval(input()))
memo = [0] * 100001
for i in range(2, 100001):
    first = prime(i)
    second = prime((i + 1) // 2)
    if first and second:
        memo[i] = 1
for i in range(1, 100001):
    memo[i] = memo[i - 1] + memo[i]
for i in range(Q):
    l, r = list(map(int, input().split()))
    print((memo[r] - memo[l - 1]))

-import math +def prime(p): +    primeFlag = True +    if p == 1: +        primeFlag = False +    for i in range(2, (int(p**0.5) + 1)): +        if p % i == 0: +            primeFlag = False +            break +    if primeFlag: +        return True +    else: +        return False -def is_prime(n): -    if n == 1: -        return False -    for k in range(2, int(math.sqrt(n)) + 1): -        if n % k == 0: -            return False -    return True - - -count = [0] -buf = 0 -for i in range(1, 10**5 + 2): -    if i % 2 == 0: -        count.append(buf) -        continue -    hantei1 = is_prime(i) -    hantei2 = is_prime((i + 1) // 2) -    if hantei1 and hantei2: -        buf += 1 -    count.append(buf) -q = int(eval(input())) -for i in range(q): +Q = int(eval(input())) +memo = [0] * 100001 +for i in range(2, 100001): +    first = prime(i) +    second = prime((i + 1) // 2) +    if first and second: +        memo[i] = 1 +for i in range(1, 100001): +    memo[i] = memo[i - 1] + memo[i] +for i in range(Q): -    print((count[r] - count[l - 1])) +    print((memo[r] - memo[l - 1]))