import sys sys.setrecursionlimit(10**6) int1 = lambda x: int(x)-1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LI1(): return list(map(int1, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = 10**16 # md = 998244353 md = 10**9+7 class LCA: # 頂点は0~n-1 def __init__(self, to, root=0): self.to = to self.n = len(to) self.parents = [-1]*(self.n+1) self.depth = [0]*self.n self.__dfs(root) self.max_level = max(self.depth).bit_length() self.ancestor = [self.parents]+[[-1]*(self.n+1) for _ in range(self.max_level)] row0 = self.ancestor[0] for lv in range(self.max_level): row1 = self.ancestor[lv+1] for u in range(self.n): row1[u] = row0[row0[u]] row0 = row1 def __dfs(self, root): stack = [root] while stack: u = stack.pop() pu = self.parents[u] du = self.depth[u] for v, _ in self.to[u]: if v == pu: continue self.parents[v] = u self.depth[v] = du+1 stack.append(v) # 最小共通祖先 def anc(self, u, v): diff = self.depth[u]-self.depth[v] if diff < 0: u, v = v, u diff = abs(diff) lv = 0 while diff: if diff & 1: u = self.ancestor[lv][u] lv, diff = lv+1, diff >> 1 if u == v: return u for lv in range(self.depth[u].bit_length()-1, -1, -1): anclv = self.ancestor[lv] if anclv[u] != anclv[v]: u, v = anclv[u], anclv[v] return self.parents[u] n = II() to = [[] for _ in range(n)] for _ in range(n-1): u, v, w = LI() to[u].append((v, w)) to[v].append((u, w)) lca = LCA(to) stack = [(0, -1)] cost = [0]*n while stack: u, pu = stack.pop() for v, w in to[u]: if v == pu: continue cost[v] = cost[u]+w stack.append((v, u)) for _ in range(II()): x, y, z = LI() a=lca.anc(x,y) b=lca.anc(x,z) c=lca.anc(z,y) ans = cost[x]+cost[y]+cost[z]-cost[a]-cost[b]-cost[c] print(ans)