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)] dij = [(0, 1), (1, 0), (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.cost = [0]*self.n 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, c in self.to[u]: if v == pu: continue self.parents[v] = u self.depth[v] = du+1 self.cost[v] = self.cost[u]+c 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] def get_cost(self, u, v): w = self.anc(u, v) return self.cost[u]+self.cost[v]-2*self.cost[w] n = II() to = [[] for _ in range(n)] for _ in range(n-1): u, v, c = LI1() to[u].append((v, c+1)) to[v].append((u, c+1)) lca = LCA(to) for _ in range(II()): u, v = LI1() print(lca.get_cost(u, v))