import sys from collections import deque def main(): input = sys.stdin.read().split() ptr = 0 N, Q = int(input[ptr]), int(input[ptr+1]) ptr += 2 # Build adjacency list adj = [[] for _ in range(N+1)] # 1-based for _ in range(N-1): a = int(input[ptr]) b = int(input[ptr+1]) adj[a].append(b) adj[b].append(a) ptr += 2 # Precompute answers for each query ans = [] # To track the current value of each node values = [0] * (N + 1) for _ in range(Q): X = int(input[ptr]) Y = int(input[ptr+1]) Z = int(input[ptr+2]) ptr += 3 # Output current value of X ans.append(str(values[X])) # BFS to find all nodes within Y distance from X visited = [False] * (N + 1) q = deque() q.append((X, 0)) visited[X] = True nodes_to_update = [] while q: u, d = q.popleft() nodes_to_update.append(u) if d < Y: for v in adj[u]: if not visited[v]: visited[v] = True q.append((v, d+1)) # Update the values for u in nodes_to_update: values[u] += Z print('\n'.join(ans)) if __name__ == '__main__': main()