#include using namespace std; class HeavyLightDecomposition{ protected: int V; vector> G; vector stsize, parent, pathtop, depth, in, reverse_in, out; int root; private: // Subtree Size void buildStsize(int curr, int prev){ stsize[curr] = 1, parent[curr] = prev; for(int &v : G[curr]){ if(v == prev){ if(v == G[curr].back()) break; else swap(v, G[curr].back()); } buildStsize(v, curr); stsize[curr] += stsize[v]; if(stsize[v] > stsize[G[curr][0]]){ swap(v, G[curr][0]); } } } void buildPath(int curr, int prev, int &t){ in[curr] = t++; reverse_in[in[curr]] = curr; for(int v : G[curr]){ if(v == prev) continue; if(v == G[curr][0]){ pathtop[v] = pathtop[curr]; } else{ pathtop[v] = v; } depth[v] = depth[curr] + 1; buildPath(v, curr, t); } out[curr] = t; } public: HeavyLightDecomposition(int node_size) : V(node_size), G(V), stsize(V, 0), parent(V, -1), pathtop(V, -1), depth(V, 0), in(V, -1), reverse_in(V, -1), out(V, -1){} void add_edge(int u, int v){ G[u].push_back(v); G[v].push_back(u); } void build(int _root = 0){ root = _root; int t = 0; buildStsize(root, -1); pathtop[root] = root; buildPath(root, -1, t); } inline int get(int a){ return in[a]; } int la(int a, int k) { while(true){ int u = pathtop[a]; if(in[a] - k >= in[u]) return reverse_in[in[a] - k]; k -= in[a] - in[u] + 1; a = parent[u]; } } int lca(int a, int b){ int pa = pathtop[a], pb = pathtop[b]; while(pathtop[a] != pathtop[b]){ if(in[pa] > in[pb]){ a = parent[pa], pa = pathtop[a]; } else{ b = parent[pb], pb = pathtop[b]; } } if(in[a] > in[b]) swap(a, b); return a; } int dist(int a, int b){ return depth[a] + depth[b] - 2 * depth[lca(a, b)]; } int jump(int from, int to, int k) { if(!k) return from; int l = lca(from, to); int d = dist(from, to); if(d < k) return -1; if(depth[from] - depth[l] >= k) return la(from, k); k -= depth[from] - depth[l]; return la(to, depth[to] - depth[l] - k); } void subtree_query(int a, const function &func){ func(in[a], out[a]); } void path_query(int a, int b, const function &func, bool include_root = true, bool reverse_path = false){ vector> path; int pa = pathtop[a], pb = pathtop[b]; while(pathtop[a] != pathtop[b]){ if(in[pa] > in[pb]){ path.emplace_back(in[pa], in[a] + 1); a = parent[pa], pa = pathtop[a]; } else{ path.emplace_back(in[pb], in[b] + 1); b = parent[pb], pb = pathtop[b]; } } if(in[a] > in[b]) swap(a, b); if(include_root) path.emplace_back(in[a], in[b] + 1); else path.emplace_back(in[a] + 1, in[b] + 1); if(!reverse_path) reverse(path.begin(), path.end()); else for(auto &p : path) p = make_pair(V - p.second, V - p.first); for(auto [u, v] : path){ func(u, v); } } void path_noncommutative_query(int a, int b, const function &func, const function &func2){ int l = lca(a, b); path_query(a, l, func2, false, true); path_query(l, b, func, true, false); } }; struct AuxiliaryTree : HeavyLightDecomposition{ using super = HeavyLightDecomposition; private: void add_aux_edge(int u, int v){ T[u].emplace_back(v); T[v].emplace_back(u); } public: vector> T; AuxiliaryTree(int n) : super(n), T(n){} void build(int _root = 0){ super::build(_root); } void query(vector &vs){ assert(!vs.empty()); sort(vs.begin(), vs.end(), [&](int a, int b){ return in[a] < in[b]; }); vs.erase(unique(vs.begin(), vs.end()), vs.end()); int k = vs.size(); vector st; st.emplace_back(vs[0]); for(int i = 0; i + 1 < k; i++){ int p = lca(vs[i], vs[i + 1]); if(p != vs[i]){ int l = st.back(); st.pop_back(); while(!st.empty() && depth[p] < depth[st.back()]){ add_aux_edge(st.back(), l); l = st.back(); st.pop_back(); } if(st.empty() || st.back() != p){ st.emplace_back(p); vs.emplace_back(p); } add_aux_edge(p, l); } st.emplace_back(vs[i + 1]); } while(st.size() > 1){ int c = st.back(); st.pop_back(); add_aux_edge(st.back(), c); } } void clear(const vector &ws){ for(int w : ws){ T[w].clear(); } } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; AuxiliaryTree G(n); vector u(n - 1), v(n - 1), w(n - 1); for(int i = 0; i < n - 1; i++){ cin >> u[i] >> v[i] >> w[i]; G.add_edge(u[i], v[i]); } G.build(); vector a(n); for(int i = 0; i < n - 1; i++){ int idx = max(G.get(u[i]), G.get(v[i])); a[idx] = w[i]; } vector cs(n + 1); for(int i = 0; i < n; i++){ cs[i + 1] = cs[i] + a[i]; } long long ans = 0; auto query = [&](int l, int r){ ans += cs[r] - cs[l]; }; auto dfs = [&](auto self, int curr, int prev) -> void { for(auto v : G.T[curr]){ if(v == prev) continue; G.path_query(curr, v, query, false, false); self(self, v, curr); } }; int q; cin >> q; while(q--){ int k = 3; vector x(k); for(int i = 0; i < k; i++) cin >> x[i]; G.query(x); ans = 0; dfs(dfs, x[0], -1); G.clear(x); cout << ans << "\n"; } }