#lca verify import sys sys.setrecursionlimit(10**7) class LCA_Tree(): # 頂点は0スタートでも1スタートでもいい # 頂点の個数だけは記録しておく。 セグメント木の実装verをしてもいいかもしれないけど、こっちのが十分早そうw def bfs(self, node): # 初期化補助関数 self.visited[node] = True for (p,w) in tree[node]: if self.visited[p] == False: self.kprv[0][p] = node self.depth[p] = self.depth[node]+1 self.real_depth[p]=self.real_depth[node]+w self.bfs(p) def __init__(self, root, tree): self.root = root self.n = len(tree)-1 self.K = (self.n-1).bit_length() self.visited = [False for i in range(self.n+1)] self.depth = [0 for i in range(self.n+1)] self.real_depth=[0 for i in range(self.n+1)] self.kprv = [[-1 for i in range(self.n+1)] for j in range(self.K+1)] self.bfs(self.root) for cnt in range(1,self.K+1): for node in range(self.n+1): if self.kprv[cnt-1][node] >= 0: self.kprv[cnt][node] = self.kprv[cnt - 1][self.kprv[cnt-1][node]] else: self.kprv[cnt][node] = -1 def query(self, u, v): # u,vの最小共通祖先 dd = self.depth[v]-self.depth[u] if dd < 0: u, v = v, u dd = -dd for i in range(self.K+1): if dd & 1: v = self.kprv[i][v] dd >>= 1 if u == v: return u for i in range(self.K-1, -1, -1): pu = self.kprv[i][u] pv = self.kprv[i][v] if pu != pv: u = pu v = pv return self.kprv[0][u] N = int(input()) tree = [[] for i in range(N)] for i in range(N-1): u, v, w = map(int, input().split()) tree[u].append((v, w)) tree[v].append((u, w)) lca = LCA_Tree(0, tree) Q = int(input()) que = [tuple(map(int, input().split())) for i in range(Q)] for a, b, c in que: x=lca.query(a, b) y=lca.query(b, c) z=lca.query(c, a) ans = 0 ans += lca.real_depth[a]+lca.real_depth[b]-2*lca.real_depth[x] ans += lca.real_depth[b]+lca.real_depth[c]-2*lca.real_depth[y] ans += lca.real_depth[c]+lca.real_depth[a]-2*lca.real_depth[z] print(ans//2)