import scala.collection.mutable

object Main extends App {

  import scala.io.StdIn._
  import scala.math._

  val Array(n, m, r, q) = readLine.trim.split(' ').map(_.toInt)
  val cards = Array.tabulate(n){_ ⇒ Nil: List[(Int, Int)]}
  for (_ ← 0 until m){
    val Array(a, b, c) = readLine.trim.split(' ').map(_.toInt)
    cards(b - 1)::=(a - 1, c)
  }
  val minChangeCost = Array.tabulate(n){_ ⇒ Long.MaxValue}
  val queue = mutable.PriorityQueue()(Ordering.by[(Int, Long), Long](_._2).reverse)
  for (i ← (r - 1) until n by r) {
    minChangeCost(i) = 0L
    queue.enqueue((i, 0L))
  }
  while (queue.nonEmpty){
    val (current, distance) = queue.dequeue()
    if (minChangeCost(current) == distance){
      for ((to, cost) ← cards(current)){
        if (minChangeCost(to) - distance > cost){
          minChangeCost(to) = cost + distance
          queue.enqueue((to, cost + distance))
        }
      }
    }
  }
  println(
    (for (_ ← 0 until q) yield {
      val Array(x, y, z) = readLine.trim.split(' ').map(_.toInt)
      if (minChangeCost(x - 1) < z - minChangeCost(y - 1)) z - minChangeCost(x - 1) - minChangeCost(y - 1)
      else 0L
    }).sum
  )


  implicit class Extension[T](val value :T){
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
    def let[R](func: T ⇒ R): R = func(value)
  }
}
