import scala.math.min
object Main {
  def main(args:Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    
    // 距離を初期化
    val towns, roads, fuelCapacity = sc.nextInt
    val distMap = Array.fill[Int](towns+1, towns+1){fuelCapacity+1}
    for(i <- 1 to roads) {
      val from, to, dist = sc.nextInt
      distMap(from)(to) = dist
      distMap(to)(from) = dist
    }
    // WF法で最短距離を算出
    for(k <- 1 to towns) {
      for(i <- 1 to towns) {
        for(j <- 1 to towns) {
          distMap(i)(j) = min(distMap(i)(j), distMap(i)(k) + distMap(k)(j))
        }
      }
    }
    // 無補給移動可能マップを作成
    val steps = distMap.map{_.map{v => if(v <= fuelCapacity) 1 else fuelCapacity}}
    // WF法で最短補給ステップマップを算出
    for(k <- 1 to towns) {
      for(i <- 1 to towns) {
        for(j <- 1 to towns) {
          steps(i)(j) = min(steps(i)(j), steps(i)(k) + steps(k)(j))
        }
      }
    }
    val q = sc.nextInt
    for(i <- 1 to q) {
      val s, t = sc.nextInt
      val step = steps(s)(t)
      if (step >= fuelCapacity) {
        println("-1")
      } else {
        println(step-1)
      }
    }
  }
}