import sys from collections import deque def main(): sys.setrecursionlimit(1 << 25) 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)] for _ in range(N-1): a = int(input[ptr]) b = int(input[ptr+1]) ptr += 2 adj[a].append(b) adj[b].append(a) # Initialize weights weight = [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 weight of X print(weight[X]) # BFS to find all nodes within Y distance from X visited = {} q = deque() q.append((X, 0)) visited[X] = True while q: u, d = q.popleft() if d > Y: continue # Add Z to u's weight weight[u] += Z for v in adj[u]: if v not in visited: visited[v] = True q.append((v, d + 1)) if __name__ == "__main__": main()