Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque, defaultdict
import sys
input = sys.stdin.readline
N,M = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(M)]
Q = int(input())
vdc = [list(map(int,input().split())) for i in range(Q)]
'''
N,M = 10**5,7
ab = [[1,2],[1,3],[1,4],[4,5],[5,6],[5,7],[2,3]]
Q = 2
vdc = [[6,1,1],[1,2,2]]
'''
g = defaultdict(set)
for a,b in ab:
    g[a].add(b)
    g[b].add(a)

ans = [0]*(N+1)

for v,d,c in vdc[::-1]:
    visited = set([v])
    vs = [v]
    if ans[v] == 0:
        ans[v] = c 
    for i in range(d):
        temp = []
        for pv in vs:
            for nv in g[pv]:
                if not nv in  visited:
                    visited.add(nv)
                    if ans[nv] == 0:
                        ans[nv] = c
                    temp.append(nv)
        vs = temp

for a in ans[1:]:
    print(a)






Fixed submission:
from collections import deque, defaultdict
import sys
input = sys.stdin.readline
N,M = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(M)]
Q = int(input())
vdc = [list(map(int,input().split())) for i in range(Q)]
g = defaultdict(set)

for a,b in ab:
    g[a].add(b)
    g[b].add(a)
'''
from random import randint as ri
import networkx as nx
N,M = 10**5,10**5
g = nx.gnm_random_graph(N,M)
g = nx.to_dict_of_lists(g)
g = {v+1:[nv+1 for nv in g[v]] for v in g}
Q = 20**3
vdc = [[ri(1,N),ri(0,10),ri(1,10**5)] for i in range(Q)]
'''

def dpinit(ps, val=0):
    import copy
    res = [val for i in [0]*ps[-1]]
    for i in ps[:-1][::-1]:
        res = [copy.deepcopy(res) for k in [0]*i]
    return res

ans = [0]*(N+1)
visited = dpinit([N+1,11],False)

for v,d,c in vdc[::-1]:
    if visited[v][d]:
        continue
    for t in range(d+1):
        visited[v][t] = True
    if ans[v] == 0:
        ans[v] = c 
    vs = [v]
    for i in range(d-1,-1,-1):
        temp = []
        for pv in vs:
            for nv in g[pv]:
                if not visited[nv][i]:
                    for t in range(i+1):
                        visited[nv][t] = True
                    if ans[nv] == 0:
                        ans[nv] = c
                    temp.append(nv)
        vs = temp
 
for a in ans[1:]:
    print(a)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque, defaultdict
import sys
input = sys.stdin.readline
N,M = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(M)]
Q = int(input())
vdc = [list(map(int,input().split())) for i in range(Q)]

g = defaultdict(set)
for a,b in ab:
    g[a].add(b)
    g[b].add(a)

ans = [0]*(N+1)
colored = [False]*(N+1)

for v,d,c in vdc[::-1]:
    visited = [False]*(N+1)
    visited[v] = True
    vs = [v]
    if ans[v] == 0:
        ans[v] = c 
    for i in range(d):
        temp = []
        for pv in vs:
            for nv in g[pv]:
                if not visited[nv]:
                    visited[nv] = True
                    if ans[nv] == 0:
                        ans[nv] = c
                    temp.append(nv)
        vs = temp

for a in ans[1:]:
    print(a)






Fixed submission:
from collections import deque, defaultdict
import sys
input = sys.stdin.readline
N,M = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(M)]
Q = int(input())
vdc = [list(map(int,input().split())) for i in range(Q)]
g = defaultdict(set)

for a,b in ab:
    g[a].add(b)
    g[b].add(a)
'''
from random import randint as ri
import networkx as nx
N,M = 10**5,10**5
g = nx.gnm_random_graph(N,M)
g = nx.to_dict_of_lists(g)
g = {v+1:[nv+1 for nv in g[v]] for v in g}
Q = 20**3
vdc = [[ri(1,N),ri(0,10),ri(1,10**5)] for i in range(Q)]
'''

def dpinit(ps, val=0):
    import copy
    res = [val for i in [0]*ps[-1]]
    for i in ps[:-1][::-1]:
        res = [copy.deepcopy(res) for k in [0]*i]
    return res

ans = [0]*(N+1)
visited = dpinit([N+1,11],False)

for v,d,c in vdc[::-1]:
    if visited[v][d]:
        continue
    for t in range(d+1):
        visited[v][t] = True
    if ans[v] == 0:
        ans[v] = c 
    vs = [v]
    for i in range(d-1,-1,-1):
        temp = []
        for pv in vs:
            for nv in g[pv]:
                if not visited[nv][i]:
                    for t in range(i+1):
                        visited[nv][t] = True
                    if ans[nv] == 0:
                        ans[nv] = c
                    temp.append(nv)
        vs = temp
 
for a in ans[1:]:
    print(a)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque, defaultdict
import sys
input = sys.stdin.readline
N,M = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(M)]
Q = int(input())
vdc = [list(map(int,input().split())) for i in range(Q)]

g = defaultdict(set)
for a,b in ab:
    g[a].add(b)
    g[b].add(a)

ans = [0]*(N+1)

for v,d,c in vdc[::-1]:
    targets = [v]
    visited = [False]*(N+1)
    visited[v] = True
    vs = [v]
    for i in range(d):
        temp = []
        for pv in vs:
            for nv in g[pv]:
                if not visited[nv]:
                    visited[nv] = True
                    targets.append(nv)
                    temp.append(nv)
        vs = temp
    for tv in targets:
        if ans[tv] == 0:
            ans[tv] = c

for a in ans[1:]:
    print(a)






Fixed submission:
from collections import deque, defaultdict
import sys
input = sys.stdin.readline
N,M = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(M)]
Q = int(input())
vdc = [list(map(int,input().split())) for i in range(Q)]
g = defaultdict(set)

for a,b in ab:
    g[a].add(b)
    g[b].add(a)
'''
from random import randint as ri
import networkx as nx
N,M = 10**5,10**5
g = nx.gnm_random_graph(N,M)
g = nx.to_dict_of_lists(g)
g = {v+1:[nv+1 for nv in g[v]] for v in g}
Q = 20**3
vdc = [[ri(1,N),ri(0,10),ri(1,10**5)] for i in range(Q)]
'''

def dpinit(ps, val=0):
    import copy
    res = [val for i in [0]*ps[-1]]
    for i in ps[:-1][::-1]:
        res = [copy.deepcopy(res) for k in [0]*i]
    return res

ans = [0]*(N+1)
visited = dpinit([N+1,11],False)

for v,d,c in vdc[::-1]:
    if visited[v][d]:
        continue
    for t in range(d+1):
        visited[v][t] = True
    if ans[v] == 0:
        ans[v] = c 
    vs = [v]
    for i in range(d-1,-1,-1):
        temp = []
        for pv in vs:
            for nv in g[pv]:
                if not visited[nv][i]:
                    for t in range(i+1):
                        visited[nv][t] = True
                    if ans[nv] == 0:
                        ans[nv] = c
                    temp.append(nv)
        vs = temp
 
for a in ans[1:]:
    print(a)