#include using namespace std; int main() { int64_t n; cin >> n; vector>> g(n); for (int64_t i = 0; i < n - 1; i++) { int64_t a, b, c; cin >> a >> b >> c; g[a].push_back({b, c}); g[b].push_back({a, c}); } vector depth(n), dist(n); vector> pre(30, vector(n)); { stack st; vector done(n, false); depth[0] = 0; dist[0] = 0; done[0] = true; pre[0][0] = 0; st.emplace(0); while (!st.empty()) { int64_t v = st.top(); st.pop(); for (auto &&p : g[v]) { if (done[p.first]) { continue; } depth[p.first] = depth[v] + 1; dist[p.first] = dist[v] + p.second; done[p.first] = true; pre[0][p.first] = v; st.emplace(p.first); } } for (int64_t i = 1; i < 30; i++) { for (int64_t j = 0; j < n; j++) { pre[i][j] = pre[i - 1][pre[i - 1][j]]; } } } auto lca = [&](int64_t x, int64_t y) { for (int64_t diff = max(int64_t(0), depth[y] - depth[x]), tmp = 0; diff; diff >>= 1, tmp++) { if (diff & 1) { y = pre[tmp][y]; } } for (int64_t diff = max(int64_t(0), depth[x] - depth[y]), tmp = 0; diff; diff >>= 1, tmp++) { if (diff & 1) { x = pre[tmp][x]; } } if (x == y) { return x; } else { int64_t ok = 29, ng = -1; while (1 < abs(ok - ng)) { int64_t mid = (ok + ng) / 2; ((pre[mid][x] == pre[mid][y]) ? ok : ng) = mid; } return pre[ok][x]; } }; int64_t q; cin >> q; for (int64_t _ = 0; _ < q; _++) { int64_t x, y, z; cin >> x >> y >> z; int64_t a = min({lca(x, y), lca(x, z), lca(y, z)}); int64_t b = max({lca(x, y), lca(x, z), lca(y, z)}); int64_t c = lca(a, b); if (a == b) { cout << (dist[x] - dist[c]) + (dist[y] - dist[c]) + (dist[z] - dist[c]) << endl; } else { cout << (dist[x] - dist[c]) + (dist[y] - dist[c]) + (dist[z] - dist[c]) - abs(dist[a] - dist[b]) << endl; } } return 0; }