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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.Collection;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Queue;
import java.util.LinkedList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main
{
    public static void main(String[] args)
    {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        Task solver = new Task();
        solver.solve(1, in, out);
        out.close();
    }

    static class Task
    {
        ArrayList<Task.Update> upd;
        int[] color;
        int req_dis;

        public void solve(int testNumber, InputReader in, OutputWriter out)
        {
            int n = in.readInt();
            int m = in.readInt();
            Graph g = new Graph(n);
            for (int i = 0; i < m; i++)
            {
                g.addEdge(in.readInt() - 1, in.readInt() - 1);
            }
            upd = new ArrayList<Task.Update>();
            color = new int[n];
            Arrays.fill(color, 0);
            int q = in.readInt();
            for (int i = 0; i < q; i++)
            {
                upd.add(new Task.Update(in.readInt() - 1, in.readInt(), in.readInt()));
            }
            for (int i = q - 1; i >= 0; i--)
            {
                req_dis = upd.get(i).dist;
                if (color[upd.get(i).ver] == 0)
                {
                    bfs(upd.get(i).col, 0, upd.get(i).ver, g);
                }
            }
            for (int i = 0; i < n; i++)
            {
                out.printLine(color[i]);
            }
        }

        void bfs(int col, int dis, int u, Graph g)
        {
            Queue<Task.Pair> q = new LinkedList<Task.Pair>();
            q.add(new Task.Pair(u, 0));
            color[u] = col;
            while (!q.isEmpty())
            {
                Task.Pair curr = q.poll();
                if (curr.dis >= req_dis)
                {
                    continue;
                }
                for (int c : g.getAdjacent(curr.u))
                {
                    if (color[c] == 0)
                    {
                        color[c] = col;
                        q.add(new Task.Pair(c, curr.dis + 1));
                    }
                }
            }
        }

        static class Pair
        {
            int u;
            int dis;

            Pair(int u, int dis)
            {
                this.u = u;
                this.dis = dis;
            }

        }

        static class Update
        {
            int ver;
            int dist;
            int col;

            Update(int ver, int dis, int col)
            {
                this.ver = ver;
                this.col = col;
                this.dist = dis;
            }

        }

    }

    static class OutputWriter
    {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream)
        {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer)
        {
            this.writer = new PrintWriter(writer);
        }

        public void close()
        {
            writer.close();
        }

        public void printLine(int i)
        {
            writer.println(i);
        }

    }

    static class Graph
    {
        int n;
        boolean[] visited;
        List<Integer>[] ar;

        public Graph(int n)
        {
            this.n = n;
            ar = new List[n];
            for (int i = 0; i < n; i++)
            {
                ar[i] = new ArrayList<Integer>();
            }
            visited = new boolean[n];
            Arrays.fill(visited, false);
        }

        public List<Integer> getAdjacent(int ver)
        {
            return ar[ver];
        }

        public void addEdge(int u, int v)
        {
            ar[u].add(v);
            ar[v].add(u);
        }

    }

    static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream)
        {
            this.stream = stream;
        }

        public int read()
        {
            if (numChars == -1)
            {
                throw new InputMismatchException();
            }
            if (curChar >= numChars)
            {
                curChar = 0;
                try
                {
                    numChars = stream.read(buf);
                } catch (IOException e)
                {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt()
        {
            int c = read();
            while (isSpaceChar(c))
            {
                c = read();
            }
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do
            {
                if (c < '0' || c > '9')
                {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c)
        {
            if (filter != null)
            {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c)
        {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter
        {
            public boolean isSpaceChar(int ch);

        }

    }
}