import sys sys.setrecursionlimit(10**6) input = sys.stdin.readline from collections import deque class LCA_wp(): def __init__(self,e,root=0): self.N = len(e) self.e = e self.root = root self.IN = [-1]*N self.OUT = [-1]*N self.DIST = [-1]*N self.time = 0 self.DIST[root] = 0 self.parents = [-1]*N v = deque() v.append(~root) v.append(root) while v: x = v.pop() if x>=0: self.IN[x] = self.time self.time += 1 else: x = ~x self.OUT[x] = self.time self.time += 1 continue for ix,c in e[x]: if self.DIST[ix] != -1: self.parents[x] = ix continue self.DIST[ix] = self.DIST[x] + c v.append(~ix) v.append(ix) self.depth = [(self.DIST[i],i) for i in range(N)] self.init_val = [-1]*(2*N) for i in range(N): x = self.IN[i] y = self.OUT[i] self.init_val[x] = self.depth[i] self.init_val[y] = self.depth[self.parents[i]] self.T = self.SegTree(self.init_val) def lca(self,l,r): L = self.IN[l] R = self.IN[r] if R 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res N = int(input()) e = [[] for _ in range(N)] for _ in range(N-1): a,b,c = map(int,input().split()) a -= 1 b -= 1 e[a].append((b,c)) e[b].append((a,c)) L = LCA_wp(e) for _ in range(int(input())): s,t = map(int,input().split()) s -= 1 t -= 1 print(L.distance_between(s,t))