import sys from collections import deque input = sys.stdin.buffer.readline N = int(input()) T = [[] for _ in range(N)] for _ in range(N - 1): a, b, c = map(int, input().split()) a -= 1 b -= 1 T[a].append((b, c)) T[b].append((a, c)) cost = [None] * N cost[0] = 0 depth = [None] * N depth[0] = 0 prv = [-1] * N d = deque([0]) while d: v = d.pop() cv = cost[v] dv = depth[v] for x, c in T[v]: if depth[x] is None: cost[x] = cv + c depth[x] = dv + 1 prv[x] = v d.append(x) # https://tjkendev.github.io/procon-library/python/graph/lca-doubling.html から拝借しています。 # N: 頂点数 # G[v]: 頂点vの子頂点 (親頂点は含まない) # # - construct # prv[u] = v: 頂点uの一つ上の祖先頂点v # - lca # kprv[k][u] = v: 頂点uの2^k個上の祖先頂点v # depth[u]: 頂点uの深さ (根頂点は0) LV = (N - 1).bit_length() def construct(prv): kprv = [prv] S = prv for k in range(LV): T = [0] * N for i in range(N): if S[i] is None: continue T[i] = S[S[i]] kprv.append(T) S = T return kprv def lca(u, v, kprv, depth): dd = depth[v] - depth[u] if dd < 0: u, v = v, u dd = -dd # assert depth[u] <= depth[v] for k in range(LV + 1): if dd & 1: v = kprv[k][v] dd >>= 1 # assert depth[u] == depth[v] if u == v: return u for k in range(LV - 1, -1, -1): pu = kprv[k][u]; pv = kprv[k][v] if pu != pv: u = pu; v = pv # assert kprv[0][u] == kprv[0][v] return kprv[0][u] kprv = construct(prv) Q = int(input()) for _ in range(Q): s, t = (int(x) - 1 for x in input().split()) print(cost[s] + cost[t] - 2 * cost[lca(s, t, kprv, depth)])