Problem:
Score : 700 points

Problem Statement

Squid loves painting vertices in graphs.

There is a simple undirected graph consisting of N vertices numbered 1 through N, and M edges.
Initially, all the vertices are painted in color 0. The i-th edge bidirectionally connects two vertices a_i and b_i. The length of every edge is 1.

Squid performed Q operations on this graph. In the i-th operation, he repaints all the vertices within a distance of d_i from vertex v_i, in color c_i.

Find the color of each vertex after the Q operations.

Constraints

1 ≤ N,M,Q ≤ 10^5

1 ≤ a_i,b_i,v_i ≤ N

a_i ≠ b_i

0 ≤ d_i ≤ 10

1 ≤ c_i ≤10^5

d_i and c_i are all integers.

There are no self-loops or multiple edges in the given graph.

Partial Score

200 points will be awarded for passing the testset satisfying 1 ≤ N,M,Q ≤ 2{,}000.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_{M} b_{M}
Q
v_1 d_1 c_1
:
v_{Q} d_{Q} c_{Q}

Output

Print the answer in N lines.
In the i-th line, print the color of vertex i after the Q operations.

Sample Input 1

7 7
1 2
1 3
1 4
4 5
5 6
5 7
2 3
2
6 1 1
1 2 2

Sample Output 1

2
2
2
2
2
1
0

Initially, each vertex is painted in color 0.
In the first operation, vertices 5 and 6 are repainted in color 1.
In the second operation, vertices 1, 2, 3, 4 and 5 are repainted in color 2.

Sample Input 2

14 10
1 4
5 7
7 11
4 10
14 7
14 3
6 14
8 11
5 13
8 3
8
8 6 2
9 7 85
6 9 3
6 7 5
10 3 1
12 9 4
9 6 6
8 2 3

Sample Output 2

1
0
3
1
5
5
3
3
6
1
3
4
5
3

The given graph may not be connected.

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using Enu = System.Linq.Enumerable;

public class Program
{
    int N, M;
    int[][] E, Q;
    int[] Color;
    int[] MaxRemDist;

    public void Solve()
    {
        N = Reader.Int();
        M = Reader.Int();
        E = EdgeRead(N, M);
        int NQ = Reader.Int();
        Q = Reader.IntTable(NQ);
        foreach (var q in Q) q[0]--;
        Color = new int[N];
        MaxRemDist = Enu.Repeat(-1, N).ToArray();

        Array.Reverse(Q);
        foreach (var q in Q)
            BFS(q[0], q[1], q[2]);

        Console.WriteLine(string.Join("\n", Color));
    }

    void BFS(int start, int D, int C)
    {
        if (MaxRemDist[start] >= D) return;

        var que = new Queue<int>();
        que.Enqueue(start);
        if (MaxRemDist[start] < 0) Color[start] = C;
        MaxRemDist[start] = D;

        for (; D > 0; D--)
        {
            var nextQue = new Queue<int>();
            while (que.Count > 0)
            {
                int at = que.Dequeue();
                foreach (int next in E[at])
                    if (D - 1 > MaxRemDist[next])
                    {
                        if (MaxRemDist[next] < 0) Color[next] = C;
                        MaxRemDist[next] = Math.Max(MaxRemDist[next], D - 1);
                        nextQue.Enqueue(next);
                    }
            }
            que = nextQue;
        }
    }

    int[][] EdgeRead(int V, int E = -1, int origin = 1)
    {
        if (E == -1) E = V - 1;
        var es = new List<int>[V];
        for (int i = 0; i < V; i++) es[i] = new List<int>();
        for (int i = 0; i < E; i++)
        {
            int a = Reader.Int() - origin, b = Reader.Int() - origin;
            es[a].Add(b);
            es[b].Add(a);
        }
        return es.Select(a => a.ToArray()).ToArray();
    }
}


class RangeUpdateSumSegTree
{
    public readonly int N;
    private readonly long[] V, S;
    private readonly bool[] Flag;

    public RangeUpdateSumSegTree(int n)
    {
        for (N = 2; N < n; ) N *= 2;
        V = new long[N * 2];
        S = new long[N * 2];
        Flag = new bool[N * 2];
    }

    public long Sum(int L, int R) { return Sum(0, 0, N, L, R); }
    private long Sum(int i, int currL, int currR, int L, int R)
    {
        if (currL >= R || currR <= L) return 0;
        if (currL >= L && currR <= R) return S[i];
        if (Flag[i]) return V[i] * (Math.Min(currR, R) - Math.Max(currL, L));
        int mid = (currL + currR) / 2;
        long valL = Sum(i * 2 + 1, currL, mid, L, R);
        long valR = Sum(i * 2 + 2, mid, currR, L, R);
        return valL + valR;
    }

    public void Update(int L, int R, long val) { Update(0, val, 0, N, L, R); }
    private void Update(int i, long val, int currL, int currR, int L, int R)
    {
        if (currL >= R || currR <= L) return;
        if (currL >= L && currR <= R)
        {
            Flag[i] = true;
            V[i] = val;
            S[i] = val * (currR - currL);
            return;
        }
        if (Flag[i] && currR - currL > 1)
        {
            V[i * 2 + 1] = V[i * 2 + 2] = V[i];
            S[i * 2 + 1] = S[i * 2 + 2] = (currR - currL) / 2 * V[i];
            Flag[i * 2 + 1] = Flag[i * 2 + 2] = true;
            Flag[i] = false;
        }
        int mid = (currL + currR) / 2;
        Update(i * 2 + 1, val, currL, mid, L, R);
        Update(i * 2 + 2, val, mid, currR, L, R);
        S[i] = S[i * 2 + 1] + S[i * 2 + 2];
    }
}



class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    public static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}