let q = Scanf.scanf " %d" @@ (+) 0
let rmax = ref 0
let lrs = Array.init q @@ fun _ -> Scanf.scanf " %d %d" @@ fun a b ->
  rmax := max !rmax b; a, b
let primes n =
  if n <= 1 then [|false; false|]
  else
    let flags = Array.make (n + 1) true in
    flags.(0) <- false; flags.(1) <- false;
    let i = ref 2 in
    while !i * !i <= n do
      if flags.(!i) then
        (let j = ref @@ !i + !i in
        while !j <= n do flags.(!j) <- false; j := !j + !i done);
      incr i
    done;
    flags
let sums = Array.make (!rmax + 1) 0
let ps = primes !rmax
let f n = if n mod 2 = 1 && ps.(n) && ps.((n + 1) / 2) then 1 else 0
let _ =
  for i = 0 to !rmax - 1 do sums.(i + 1) <- sums.(i) + f (i + 1) done;
  let g (l, r) = Printf.printf "%d\n" @@ sums.(r) - sums.(l - 1) in
  Array.iter g lrs