Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]

def unite(x,y):
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        if par[x] > par[y]:
            x,y = y,x
        par[x] += par[y]
        par[y] = x
        return True

def size(x):
    return -par[find(x)]
  
n, m = map(int, readline().split())
par = [-1]*n
g = [[] * n for i in range(n)]
for i in range(m):
    u, v = map(int,readline().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)
    unite(u,v)
    
q = int(readline())
chk = []
for i in range(q):
  V,D,C = map(int,readline().split())
  chk.append((V,D,C))
  
from collections import deque
ans = [0]*n
cnt = [0]*n

def bfs(s,col,d,f,ss):
  global end
  que = deque()
  que.append((s,1,col))
  if ans[s] == 0:
    ans[s] = col
    cnt[f] += 1
  while que:
      now,p,col = que.popleft()
      if cnt[f] == ss:
        break
      for i in g[now]:
          if p <= d and ans[i] == 0:
            ans[i] = col
            cnt[f] += 1
            end += 1
            if cnt[f] == ss:
              break
          if p < d:
            que.append((i,p+1,col))

end = 0
for i in chk[::-1]:
  vv,d,cc = i
  f = find(vv-1)
  ss = size(f)
  if end == n:
    break
  if cnt[f] == ss:
    continue
  bfs(vv-1,cc,d,f,ss)
    
print(*ans, sep='\n')

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(4100000)
  
n,m = map(int, readline().split())
g = [[]*n for i in range(n)]
for i in range(m):
    u,v = map(int,readline().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)
    
q = int(readline())
chk = []
for i in range(q):
  V,D,C = map(int,readline().split())
  chk.append((V,D,C))
  

memo = [[0]*11 for _ in range(n)]

def f(v,d,c):
  if memo[v][d] != 0:
    return
  else:
    memo[v][d] = c
    if d > 0:
      f(v,d-1,c)
      for nxt in g[v]:
        f(nxt,d-1,c)
    else:
      return

for V,D,C in chk[::-1]:
    f(V-1,D,C)

for v in range(n):
    print(memo[v][0])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

n, m = map(int, readline().split())
g = [[] * n for i in range(n)]
for i in range(m):
    u, v = map(int,readline().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)
    
q = int(readline())
chk = []
for i in range(q):
  V,D,C = map(int,readline().split())
  chk.append((V,D,C))
  
from collections import deque
ans = [0]*n

def bfs(s,col,d):
  global cnt
  que = deque()
  que.append((s,1,col))
  if ans[s] == 0:
    ans[s] = col
    cnt += 1
  while que:
      if cnt == n:
        break    
      now,p,col = que.popleft()
      if p > d:
        break
      for i in g[now]:
          if p <= d and ans[i] == 0:
            ans[i] = col
            cnt += 1
            if cnt == n:
              break           
          if p < d:
            que.append((i,p+1,col))

cnt = 0
for i in chk[::-1]:
  vv,d,cc = i
  bfs(vv-1,cc,d)
  if cnt == n:
    break
    
print(*ans, sep='\n')

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(4100000)
  
n,m = map(int, readline().split())
g = [[]*n for i in range(n)]
for i in range(m):
    u,v = map(int,readline().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)
    
q = int(readline())
chk = []
for i in range(q):
  V,D,C = map(int,readline().split())
  chk.append((V,D,C))
  

memo = [[0]*11 for _ in range(n)]

def f(v,d,c):
  if memo[v][d] != 0:
    return
  else:
    memo[v][d] = c
    if d > 0:
      f(v,d-1,c)
      for nxt in g[v]:
        f(nxt,d-1,c)
    else:
      return

for V,D,C in chk[::-1]:
    f(V-1,D,C)

for v in range(n):
    print(memo[v][0])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

n, m = map(int, readline().split())
g = [[] * n for i in range(n)]
for i in range(m):
    u, v = map(int,readline().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)
    
q = int(readline())
chk = []
for i in range(q):
  V,D,C = map(int,readline().split())
  chk.append((V,D,C))
  
from collections import deque
ans = [0]*n

def bfs(s,col,d):
  global cnt
  que = deque()
  que.append((s,1,col))
  if ans[s] == 0:
    ans[s] = col
    cnt += 1
  while que:
      now,p,col = que.popleft()
      if cnt == n:
        break
      for i in g[now]:
          if p <= d and ans[i] == 0:
            ans[i] = col
            cnt += 1
          if p < d:
            que.append((i,p+1,col))

cnt = 0
for i in chk[::-1]:
  vv,d,cc = i
  bfs(vv-1,cc,d)
  if cnt == n:
    break
    
print(*ans, sep='\n')

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(4100000)
  
n,m = map(int, readline().split())
g = [[]*n for i in range(n)]
for i in range(m):
    u,v = map(int,readline().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)
    
q = int(readline())
chk = []
for i in range(q):
  V,D,C = map(int,readline().split())
  chk.append((V,D,C))
  

memo = [[0]*11 for _ in range(n)]

def f(v,d,c):
  if memo[v][d] != 0:
    return
  else:
    memo[v][d] = c
    if d > 0:
      f(v,d-1,c)
      for nxt in g[v]:
        f(nxt,d-1,c)
    else:
      return

for V,D,C in chk[::-1]:
    f(V-1,D,C)

for v in range(n):
    print(memo[v][0])