#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, int parent, int dep, int64_t d) { if (i) { par[i].push_back(parent); int bit = 0; int cur = parent; while ((1 << bit) < par[cur].size()) { par[i].push_back(cur = par[cur][bit]); bit++; } } depth[i] = dep; dist[i] = d; for (auto j : hen[i]) dfs(j.first, i, dep + 1, d + j.second); } 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(); int a[n - 1], b[n - 1], c[n - 1]; hen.resize(n); for (int i = 0; i + 1 < n; i++) { a[i] = ri(); b[i] = ri(); c[i] = ri(); hen[a[i]].push_back({b[i], c[i]}); } par.resize(n); depth.resize(n); dist.resize(n); dfs(0, -1, 0, 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; }