class lowest_common_ancestor: logN = 20 def __init__(self, N): self.g = [[] for i in range(N)] self.parent = [[-1 for i in range(N)] for i in range(self.logN)] self.depth = [0 for i in range(N)] def add_edge(self, u, v): self.g[u].append(v) self.g[v].append(u) def dfs(self, v, par = -1, d = 0): self.parent[0][v] = par self.depth[v] = d for e in self.g[v]: if e == par: continue self.dfs(e, v, d + 1) def build(self): self.dfs(0) for k in range(self.logN - 1): for i in range(len(self.g)): if self.parent[k][i] == -1: self.parent[k + 1][i] = -1 else: self.parent[k + 1][i] = self.parent[k][self.parent[k][i]] def query(self, u, v): if self.depth[u] > self.depth[v]: u, v = v, u for k in range(self.logN): if (((self.depth[v] - self.depth[u]) >> k) & 1) == 1: v = self.parent[k][v] if u == v: return u for k in range(self.logN - 1, -1, -1): if self.parent[k][u] == self.parent[k][v]: continue u = self.parent[k][u] v = self.parent[k][v] return self.parent[0][u] g = [[]] dist = [] def dfs(v, par): for e in g[v]: if e[0] == par: continue dist[e[0]] = dist[v] + e[1] dfs(e[0], v) lca = lowest_common_ancestor(0) def calc(u, v): c = lca.query(u, v) return dist[u] + dist[v] - 2 * dist[c] if __name__ == "__main__": N = int(input()) g = [[] for i in range(N)] lca = lowest_common_ancestor(N) for i in range(N - 1): a, b, c = map(int, input().split()) g[a].append([b, c]) g[b].append([a, c]) lca.add_edge(a, b) lca.build() dist = [0 for i in range(N)] dfs(0, -1) Q = int(input()) for _ in range(Q): a, b, c = map(int, input().split()) print((calc(a, b) + calc(b, c) + calc(c, a)) >> 1)