import scala.collection.mutable
import scala.io.StdIn._
object Main extends App {
  val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
  val rooms = Array.fill(n){Nil: List[(Int, Long)]}
  for (_ ← 0 until m){
    val Array(a, b, c) = readLine.trim.split(' ').map(_.toInt)
    rooms(a) ::= (b, c)
    rooms(b) ::= (a, c)
  }
  val fromStart = Array.fill(n){Long.MaxValue}
  val queue = mutable.PriorityQueue()(Ordering.by[(Int, Long), Long](_._2).reverse)
  fromStart(0) = 0
  queue.enqueue((0, 0L))
  while(queue.nonEmpty) {
    val (current, cost) = queue.dequeue()
    if (fromStart(current) == cost) {
      for ((to, len) ← rooms(current)) {
        if (fromStart(to) > len + cost) {
          fromStart(to) = len + cost
          queue.enqueue((to, len + cost))
        }
      }
    }
  }
  val fromGoal = Array.fill(n){Long.MaxValue}
  fromGoal(n - 1) = 0
  queue.enqueue((n - 1, 0))
  while(queue.nonEmpty) {
    val (current, cost ) = queue.dequeue()
    if (fromGoal(current) == cost) {
      for ((to, len) ← rooms(current)) {
        if (fromGoal(to) > len + cost) {
          fromGoal(to) = len + cost
          queue.enqueue((to, len + cost))
        }
      }
    }
  }
  val seg = Seg(fromStart.zip(fromGoal))
  for (_ ← 0 until readInt()) {
    val Array(s, g) = readLine.trim.split(' ').map(_.toLong)
    println(seg(s, g))
  }
}

object Seg {
  def apply(array: Array[(Long, Long)]): Seg = {
    val a = array.sortBy(_._1)
    val b = a.map{p ⇒ Array(p._2)}
    for (i ← b.indices) {
      val up = i + (~i & (i + 1))
      if (b.indices.contains(up)) {
        b(up) ++= b(i)
      }
      b(i) = b(i).sorted
    }
    new Seg(b, a.map{_._1})
  }
}
class Seg private (private val array: Array[Array[Long]], private val values: Array[Long]) {
  def apply(near: Long, far: Long): Int = {
    count(upperBound(values, near) - 1, far)
  }
  private[this] def upperBound(a: Array[Long], value: Long): Int = {
    var left = 0
    var right = a.length
    while (left < right) {
      val mid = (left + right) / 2
      if (a(mid) <= value) left = mid + 1
      else right = mid
    }
    right
  }
  private[this] def lowerBound(a: Array[Long], value: Long): Int = {
    var left = 0
    var right = a.length
    while(left < right){
      val mid = (left + right) / 2
      if (a(mid) < value) left = mid + 1
      else right = mid
    }
    right
  }
  def count(position: Int, value: Long): Int = {
    if (position >= 0){
      array(position).length - lowerBound(array(position), value) + count(position - (~position & (position + 1)), value)
    }else 0
  }
}
