let fst3 (a,b,c) = a
let () = Scanf.scanf "%d %d" @@ fun n m ->
  let es = Array.init m (fun _ -> Scanf.scanf " %d %d" @@ fun a b -> a-1, b-1) in
  Scanf.scanf " %d" @@ fun q ->

  let qa =
    let t = Array.init 11 (fun _ -> []) in
    for i = 0 to q-1 do
      Scanf.scanf " %d %d %d" @@ fun v d c ->
      t.(10-d) <- (i, v-1, c) :: t.(10-d)
    done;
    Array.map List.rev t
  in

  let color = Array.make n (-1, 0, -1) in
  qa.(0) |> List.iter (fun (i, v, c) -> color.(v) <- i, c, 0);
  for r = 1 to 10 do
    Array.sort (fun (a,b) (c,d) ->
      min (fst3 color.(a)) (fst3 color.(b)) - min (fst3 color.(c)) (fst3 color.(d))) es;
    Array.iter (fun (u, v) ->
      let iu, cu, ru = color.(u) in
      let iv, cv, rv = color.(v) in
      if iu > iv && ru < r then color.(v) <- (iu, cu, ru+1);
      if iv > iu && rv < r then color.(u) <- (iv, cv, rv+1);
    ) es;
    qa.(r) |> List.iter (fun (i, v, c) ->
      let ia, _, _ = color.(v) in
      if i > ia then color.(v) <- (i, c, r));
  done;

  Array.iter (fun (_, c, _) -> Printf.printf "%d\n" c) color