(* グラフ上の各点の最短距離を求める *)
(* グラフ上の点の数 n と 各辺の長さ e を引数に取る *)
(* 辺が存在しない場合は各辺の長さに10000とかを入れておく *)
let warshallFloydAlgorithm : int -> int array array -> int array array = fun n e ->
  let d = Array.make_matrix n n 0 in
  for i = 0 to n - 1 do
    for j = 0 to n - 1 do
      d.(i).(j) <- e.(i).(j)
    done
  done;

  for k = 0 to n - 1 do
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
        if (d.(i).(j) > d.(i).(k) + d.(k).(j)) then
          d.(i).(j) <- d.(i).(k) + d.(k).(j)
      done
    done;
  done;
  d
  
let () =
  let (n, m, l) = Scanf.sscanf (input_line stdin) "%d %d %d" (fun a b c -> (a, b, c)) in
  let e = Array.make_matrix n n 100000000000 in
  for i = 0 to m - 1 do
    let (a, b, c) = Scanf.sscanf (input_line stdin) "%d %d %d" (fun a b c -> (a, b, c)) in
    e.(a - 1).(b - 1) <- c;
    e.(b - 1).(a - 1) <- c;
  done;
  for i = 0 to n - 1 do
    e.(i).(i) <- 0
  done;
  let d = warshallFloydAlgorithm n e in
  let ones = Array.make_matrix n n 100000000000 in
  for i = 0 to n - 1 do
    ones.(i).(i) <- 0
  done;
  for i = 0 to n - 1 do
    for j = 0 to n - 1 do
      if (d.(i).(j) <= l) then
        ones.(i).(j) <- 1
    done
  done;
  let ans = warshallFloydAlgorithm n ones in
  let q = Scanf.sscanf (input_line stdin) "%d" (fun a -> a) in
  let ansl = Array.make q (-1) in
  for i = 0 to q - 1 do
    let (s, t) = Scanf.sscanf (input_line stdin) "%d %d" (fun a b -> (a, b)) in 
    if ans.(s - 1).(t - 1) < 100000000000 then
      ansl.(i) <-  ans.(s - 1).(t - 1) - 1
  done;
  for i = 0 to q - 1 do
    print_int ansl.(i);
    print_newline ()
  done;
