Buggy python submission (verdict: Time Limit Exceeded):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import defaultdict, deque
    n, m = map(int, readline().split())
    edge = defaultdict(list)
    for _ in range(m):
        a, b = map(int, readline().split())
        edge[a].append(b)
        edge[b].append(a)

    q = int(readline())
    mat = [[] for _ in range(q)]

    for i in range(q):
        mat[i] = list(map(int, readline().split()))

    paint = [[0, -1] for _ in range(n + 1)]
    outer = defaultdict(list)

    for v, d, c in mat[::-1]:
        if d == 0 and paint[v] == -1:
            paint[v][0] = c
            paint[v][1] = 0
        else:
            if paint[v][1] >= d:
                continue
            que = deque()
            que.append((v, d, c))
            visited = [False] * (n+1)
            while que:
                cur, rem, color = que.popleft()
                visited[cur] = True
                if paint[cur][0] == 0:
                    paint[cur][0] = color
                    paint[cur][1] = rem
                    outer[v].append(cur)
                if rem > 0:
                    n_rem = rem - 1
                    for nx in edge[cur]:
                        if visited[nx]:
                            continue
                        if paint[nx][1] >= n_rem:
                            pass
                        else:
                            que.append((nx, n_rem, color))

    for i in range(1, n + 1):
        print(paint[i][0])


if __name__ == '__main__':
    main()


Fixed submission:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import defaultdict, deque
    n, m = map(int, readline().split())
    edge = defaultdict(list)
    for _ in range(m):
        a, b = map(int, readline().split())
        edge[a].append(b)
        edge[b].append(a)

    q = int(readline())
    mat = [[] for _ in range(q)]

    for i in range(q):
        mat[i] = list(map(int, readline().split()))

    dp = [[0] * 11 for _ in range(n + 1)]

    for v, d, c in mat[::-1]:
        if dp[v][d] != 0:
            continue
        que = deque()
        que.append((v, d, c))
        while que:
            cur, dist, color = que.popleft()
            for i in range(dist, -1, -1):
                if dp[cur][i] == 0:
                    dp[cur][i] = color
                else:
                    break
            if dist > 0:
                for nx in edge[cur]:
                    if dp[nx][dist - 1] != 0:
                        continue
                    else:
                        que.append((nx, dist - 1, color))

    for i in range(1, n + 1):
        print(dp[i][0])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import defaultdict, deque
    n, m = map(int, readline().split())
    edge = defaultdict(list)
    for _ in range(m):
        a, b = map(int, readline().split())
        edge[a].append(b)
        edge[b].append(a)

    q = int(readline())
    mat = [[] for _ in range(q)]

    for i in range(q):
        mat[i] = list(map(int, readline().split()))

    paint = [[0, -1] for _ in range(n + 1)]
    for v, d, c in mat[::-1]:
        if d == 0 and paint[v] == -1:
            paint[v][0] = c
            paint[v][1] = 0
        else:
            if paint[v][1] >= d:
                continue
            que = deque()
            que.append((v, d, c))
            while que:
                cur, rem, color = que.popleft()
                if paint[cur][0] == 0:
                    paint[cur][0] = color
                    paint[cur][1] = rem
                if rem > 0:
                    n_rem = rem - 1
                    for nx in edge[cur]:
                        if paint[nx][1] >= n_rem:
                            pass
                        else:
                            que.append((nx, n_rem, color))

    for i in range(1, n + 1):
        print(paint[i][0])


if __name__ == '__main__':
    main()


Fixed submission:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import defaultdict, deque
    n, m = map(int, readline().split())
    edge = defaultdict(list)
    for _ in range(m):
        a, b = map(int, readline().split())
        edge[a].append(b)
        edge[b].append(a)

    q = int(readline())
    mat = [[] for _ in range(q)]

    for i in range(q):
        mat[i] = list(map(int, readline().split()))

    dp = [[0] * 11 for _ in range(n + 1)]

    for v, d, c in mat[::-1]:
        if dp[v][d] != 0:
            continue
        que = deque()
        que.append((v, d, c))
        while que:
            cur, dist, color = que.popleft()
            for i in range(dist, -1, -1):
                if dp[cur][i] == 0:
                    dp[cur][i] = color
                else:
                    break
            if dist > 0:
                for nx in edge[cur]:
                    if dp[nx][dist - 1] != 0:
                        continue
                    else:
                        que.append((nx, dist - 1, color))

    for i in range(1, n + 1):
        print(dp[i][0])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import defaultdict, deque
    n, m = map(int, readline().split())
    edge = defaultdict(list)
    for _ in range(m):
        a, b = map(int, readline().split())
        edge[a].append(b)
        edge[b].append(a)

    mat = []
    q = int(readline())
    for _ in range(q):
        mat.append(list(map(int, readline().split())))

    paint = [[0, -1] for _ in range(n + 1)]
    for v, d, c in mat[::-1]:
        if d == 0 and paint[v] == -1:
            paint[v][0] = c
            paint[v][1] = 0
        else:
            que = deque()
            que.append((v, d, c))
            while que:
                cur, rem, color = que.popleft()
                if paint[cur][0] == 0:
                    paint[cur][0] = color
                    paint[cur][1] = rem
                if rem > 0:
                    n_rem = rem - 1
                    for nx in edge[cur]:
                        if paint[nx][1] >= n_rem:
                            pass
                        else:
                            que.append((nx, n_rem, color))

    for i in range(1, n + 1):
        print(paint[i][0])


if __name__ == '__main__':
    main()


Fixed submission:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import defaultdict, deque
    n, m = map(int, readline().split())
    edge = defaultdict(list)
    for _ in range(m):
        a, b = map(int, readline().split())
        edge[a].append(b)
        edge[b].append(a)

    q = int(readline())
    mat = [[] for _ in range(q)]

    for i in range(q):
        mat[i] = list(map(int, readline().split()))

    dp = [[0] * 11 for _ in range(n + 1)]

    for v, d, c in mat[::-1]:
        if dp[v][d] != 0:
            continue
        que = deque()
        que.append((v, d, c))
        while que:
            cur, dist, color = que.popleft()
            for i in range(dist, -1, -1):
                if dp[cur][i] == 0:
                    dp[cur][i] = color
                else:
                    break
            if dist > 0:
                for nx in edge[cur]:
                    if dp[nx][dist - 1] != 0:
                        continue
                    else:
                        que.append((nx, dist - 1, color))

    for i in range(1, n + 1):
        print(dp[i][0])


if __name__ == '__main__':
    main()