Problem:
Score : 400 points

Problem Statement

We say that a odd number N is similar to 2017 when both N and (N+1)/2 are prime.

You are given Q queries.

In the i-th query, given two odd numbers l_i and r_i, find the number of odd numbers x similar to 2017 such that l_i ≤ x ≤ r_i.

Constraints

1≤Q≤10^5

1≤l_i≤r_i≤10^5

l_i and r_i are odd.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line (1≤i≤Q) should contain the response to the i-th query.

Sample Input 1

1
3 7

Sample Output 1

2

3 is similar to 2017, since both 3 and (3+1)/2=2 are prime.

5 is similar to 2017, since both 5 and (5+1)/2=3 are prime.

7 is not similar to 2017, since (7+1)/2=4 is not prime, although 7 is prime.

Thus, the response to the first query should be 2.

Sample Input 2

4
13 13
7 11
7 11
2017 2017

Sample Output 2

1
0
0
1

Note that 2017 is also similar to 2017.

Sample Input 3

6
1 53
13 91
37 55
19 51
73 91
13 49

Sample Output 3

4
4
1
1
1
2

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::io;

fn read_line() -> String {
    let mut in_str = String::new();
    io::stdin().read_line(&mut in_str).unwrap();
    return in_str
}

fn read_words_in_line() -> Vec<String> {
    let s = read_line();
    return s.split_whitespace().map(|x| x.to_string()).collect::<Vec<String>>()
}

fn read_t_in_line<T : std::str::FromStr>() -> Vec<T> {
    let mut v : Vec<T> = Vec::new();
    for e in read_words_in_line().iter().map(|x| x.parse::<T>()) {
        if let Ok(r) = e {
            v.push(r);
        }
        else{
            return Vec::new();
        }
    }
    return v;
}

fn sieve(upper_bound : usize) -> (Vec<i32>, Vec<bool>) {
    // list up primes less than or equal to upper_bound
    // return a prime list and a list which element has true if its index is prime 
    let mut prime_list : Vec<i32>  = Vec::new();
    let mut is_prime               = vec![true ; upper_bound + 1];
    is_prime[0] = false;
    is_prime[1] = false;
    for i in 0..upper_bound + 1 {
        if is_prime[i] {
            prime_list.push(i as i32);
            let mut mul = i + i;
            while mul <= upper_bound {
                is_prime[mul] = false;
                mul += i;
            }
        }
    }
    return (prime_list, is_prime);
}


fn main(){

    const MAX_N : usize = 100010;
    
    let mut ls = [0 ; MAX_N];
    let mut rs = [0 ; MAX_N];

    let v1 = read_t_in_line::<i32>();
    let q : usize = v1[0] as usize;

    for i in 0..q {
        let v2 = read_t_in_line::<i32>();
        ls[i] = v2[0];
        rs[i] = v2[1];
    }

    // calculate prime list
    let (_, is_prime) = sieve(MAX_N);
    
    let mut is_like_2017 = [false ; MAX_N];
    for i in 0..MAX_N {
        if i % 2 == 1 && is_prime[i] && is_prime[(i + 1) / 2] {
            is_like_2017[i] = true;
        }
    }
    
    let mut summed = [0 ; MAX_N];
    for i in 0..(MAX_N-1) {
        summed[i+1] = summed[i] + is_like_2017[i] as i32;
    }

    for i in 0..q {
        let l = ls[i] as usize;
        let r = rs[i] as usize;
        println!("{}", summed[r + 1] - summed[l]);
    }
    
    

}