typedef long long ll; #include using namespace std; template struct SegTree { using Func = function; const Func F; const Monoid UNITY; int SIZE_R; vector dat; SegTree(int n, const Func f, const Monoid &unity): F(f), UNITY(unity) { init(n); } void init(int n) { SIZE_R = 1; while (SIZE_R < n) SIZE_R *= 2; dat.assign(SIZE_R * 2, UNITY); } /* set, a is 0-indexed */ void set(int a, const Monoid &v) { dat[a + SIZE_R] = v; } void build() { for (int k = SIZE_R - 1; k > 0; --k) dat[k] = F(dat[k*2], dat[k*2+1]); } /* update a, a is 0-indexed */ void update(int a, const Monoid &v) { int k = a + SIZE_R; dat[k] = v; while (k >>= 1) dat[k] = F(dat[k*2], dat[k*2+1]); } /* get [a, b), a and b are 0-indexed */ Monoid get(int a, int b) { Monoid vleft = UNITY, vright = UNITY; for (int left = a + SIZE_R, right = b + SIZE_R; left < right; left >>= 1, right >>= 1) { if (left & 1) vleft = F(vleft, dat[left++]); if (right & 1) vright = F(dat[--right], vright); } return F(vleft, vright); } inline Monoid operator [] (int a) { return dat[a + SIZE_R]; } /* debug */ void print() { for (int i = 0; i < SIZE_R; ++i) { cout << (*this)[i]; if (i != SIZE_R-1) cout << ","; } cout << endl; } }; using Graph = vector >; struct LCA { vector > parent; // parent[d][v] := 2^d-th parent of v vector depth; LCA() { } LCA(const Graph &G, int r = 0) { init(G, r); } void init(const Graph &G, int r = 0) { int V = (int)G.size(); int h = 1; while ((1<(V, -1)); depth.assign(V, -1); dfs(G, r, -1, 0); for (int i = 0; i+1 < (int)parent.size(); ++i) for (int v = 0; v < V; ++v) if (parent[i][v] != -1) parent[i+1][v] = parent[i][parent[i][v]]; } void dfs(const Graph &G, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto e : G[v]) if (e != p) dfs(G, e, v, d+1); } int get(int u, int v) { if (depth[u] > depth[v]) swap(u, v); for (int i = 0; i < (int)parent.size(); ++i) if ( (depth[v] - depth[u]) & (1<= 0; --i) { if (parent[i][u] != parent[i][v]) { u = parent[i][u]; v = parent[i][v]; } } return parent[0][u]; } }; int main() { ll n; std::cin >> n; vector>> edges(n); Graph G(n); for (int i = 0; i < n-1; i++) { ll a,b,c; std::cin >> a>>b>>c; edges[b].push_back({a,c}); edges[a].push_back({b,c}); G[a].push_back(b); G[b].push_back(a); } LCA lca(G); vector dist(n,0); function rec = [&] (int par, int cur) { for (auto e : edges[cur]) { if(e.first==par)continue; dist[e.first] = dist[cur]+e.second; rec(cur, e.first); } }; rec(-1,0); ll q; std::cin >> q; for (int i = 0; i < q; i++) { ll x,y,z; std::cin >> x>>y>>z; cout<