import java.util.Scanner

import scala.collection.mutable

object MathAlgo {
  def Primes(size: Int): Array[Int] = {
    var primes = new Array[Int](0)
    val flag = Array.fill[Boolean](size)(true)
    val limit = Math.sqrt(size).toInt

    (2 until limit).foreach(i => {
      if (flag(i)) {
        primes = primes :+ i
        (i until size by i).foreach(j => {
          flag(j) = false
        })
      }
    })

    (limit until size).foreach(i => {
      if (flag(i)) {
        primes = primes :+ i
      }
    })

    primes
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val P: Set[Int] = MathAlgo.Primes(200002).toSet
    var l2017nums: mutable.HashSet[Int] = new mutable.HashSet[Int]

    P.foreach(p => {
      if (P.contains((p+1)/2)) {
        l2017nums = l2017nums + p
      }
    })

    val Q = sc.nextInt
    (1 to Q).foreach(i => {
      var ans = 0
      val l, r = sc.nextInt
      (l to r).foreach(j => {
        if (l2017nums.contains(j)) {
          ans += 1
        }
      })

      println(ans)
    })
  }
}