import sys from collections import deque def main(): input = sys.stdin.read().split() idx = 0 N, Q = int(input[idx]), int(input[idx+1]) idx += 2 # Build adjacency list adj = [[] for _ in range(N+1)] # nodes are 1-based for _ in range(N-1): a = int(input[idx]) b = int(input[idx+1]) adj[a].append(b) adj[b].append(a) idx += 2 # Initialize node values values = [0] * (N + 1) for _ in range(Q): X = int(input[idx]) Y = int(input[idx+1]) Z = int(input[idx+2]) idx += 3 # Output current value of X print(values[X]) # BFS to update nodes within Y distance visited = [False] * (N + 1) q = deque() q.append((X, 0)) visited[X] = True while q: u, d = q.popleft() if d > Y: continue values[u] += Z for v in adj[u]: if not visited[v] and d + 1 <= Y: visited[v] = True q.append((v, d + 1)) if __name__ == "__main__": main()