import java.util.Scanner

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)

    val q = sc.nextInt

    val qPairs = Seq.fill(q)((sc.nextInt, sc.nextInt))

    def isPrime(num: Int): Boolean = {
      num match {
        case n if n <= 1 => false
        case 2 => true
        case n =>
          (2 to Math.sqrt(n).toInt + 1).forall { i =>
            n % i != 0
          }
      }
    }

    def is2017LikeNumber(num: Int): Boolean = isPrime(num) && isPrime((num + 1)/ 2)

    val res = qPairs.map{case (l,r) =>
      (l to r).count(is2017LikeNumber)
    }

    res.foreach(println(_))

  }
}
