#include using namespace std; using ll = long long; using Pll = pair; using Pii = pair; constexpr ll MOD = 1000000007; constexpr long double EPS = 1e-10; constexpr int dyx[4][2] = { { 0, 1}, {-1, 0}, {0,-1}, {1, 0} }; constexpr int N_MAX = 100000; vector graph[N_MAX]; vector sum_cost(N_MAX, 0); class LowestCommonAncestor { public: int root, n, LOG_V_MAX = 30; vector depth; vector> parent; LowestCommonAncestor(int root=0, int n=N_MAX): root(root), n(n) { depth.assign(n, -1); parent.assign(n, vector(LOG_V_MAX, -1)); dfs(root, -1, 0); for(int j=1;j depth[v]) swap(u, v); int depth_diff = depth[v] - depth[u]; for(int j=0;j=0;--j) { if(parent[u][j] != parent[v][j]) { u = parent[u][j]; v = parent[v][j]; } } return parent[u][0]; } }; void dfs_cost(int node, int par, ll s) { sum_cost[node] = s; for(Pll child: graph[node]) { if(child.first == par) continue; dfs_cost(child.first, node, s+child.second); } } inline ll calc(int x, int y, int z, LowestCommonAncestor &lca) { return sum_cost[x] + sum_cost[y] + sum_cost[z] - sum_cost[lca.get_lca(x, y)] - sum_cost[lca.get_lca(y, z)] - sum_cost[lca.get_lca(z, x)]; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int u, v; ll w; for(int i=0;i> u >> v >> w; graph[u].emplace_back(v, w); graph[v].emplace_back(u, w); } LowestCommonAncestor lca = LowestCommonAncestor(0, n); dfs_cost(0, -1, 0); int q, x, y, z; cin >> q; while(q--) { cin >> x >> y >> z; cout << calc(x, y, z, lca) << endl; } }