import sys readline = sys.stdin.readline from collections import deque def f(G, s, N): Q = deque([]) dist = [-1] * N par = [-1] * N depth = [0] * N dist[s] = 0 for u, c in G[s]: par[u] = s dist[u] = c depth[u] = 1 Q.append(u) while Q: u = Q.popleft() for v, c in G[u]: if dist[v] != -1: continue dist[v] = dist[u] + c depth[v] = depth[u] + 1 par[v] = u Q.append(v) return dist, par, depth def doubling(X): M = 35 dp = [[0] * len(X) for i in range(M)] dp[0] = X for i in range(M - 1): for j in range(len(X)): dp[i + 1][j] = dp[i][dp[i][j]] return dp N = int(readline()) G = [[] for i in range(N)] for i in range(N - 1): a, b, c = map(int, readline().split()) a, b = a - 1 ,b - 1 G[a].append((b, c)) G[b].append((a, c)) D, P, DD = f(G, 0, N) dp = doubling(P) Q = int(readline()) for _ in range(Q): s, t = map(int, readline().split()) s, t = s - 1, t - 1 ps, pt = s, t if DD[s] > DD[t]: s, t = t, s d = DD[t] - DD[s] for i in range(30): if (d >> i) & 1: t = dp[i][t] if s == t: LCA = s else: for i in range(30, -1, -1): if dp[i][s] != dp[i][t]: s = dp[i][s] t = dp[i][t] LCA = P[s] print(D[ps] + D[pt] - 2 * D[LCA])