#include int ri() { int n; scanf("%d", &n); return n; } std::vector > > hen; std::vector depth; std::vector dist; std::vector > par; void dfs(int i) { if (i) { int cur = par[i][0]; for (int bit = 0; bit < par[cur].size(); bit++) par[i].push_back(cur = par[cur][bit]); } for (auto j : hen[i]) { par[j.first].push_back(i); dist[j.first] = dist[i] + j.second; depth[j.first] = depth[i] + 1; dfs(j.first); } } int lca(int i, int j) { if (i == j) return i; if (depth[i] > depth[j]) std::swap(i, j); for (int k = 20; k >= 0; k--) if (depth[j] - (1 << k) >= depth[i]) j = par[j][k]; if (i == j) return i; for (int k = 20; k >= 0; k--) if (k < par[i].size() && par[i][k] != par[j][k]) { i = par[i][k]; j = par[j][k]; } return par[i][0]; } int main() { int n = ri(); hen.resize(n); for (int i = 0; i + 1 < n; i++) { int a = ri(), b = ri(), c = ri(); hen[a].push_back({b, c}); } par.resize(n); depth.resize(n); dist.resize(n); dfs(0); int q = ri(); for (int i = 0; i < q; i++) { int x = ri(); int y = ri(); int z = ri(); int l1 = lca(x, y); int l2 = lca(y, z); int l3 = lca(x, z); int64_t res = dist[x] + dist[y] + dist[z] - (dist[l1] + dist[l2] + dist[l3]); std::cout << res << std::endl; } return 0; }