#include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; typedef pair P; const int INF = 1001001001; struct edge { int to, cost; }; vector g[100100]; vector depth; vector cost; vector > parent; int n; void dfs(int v, int p=-1, int d=0) { depth[v] = d; parent[0][v] = p; for (auto e: g[v]) { if (e.to == p) continue; cost[e.to] = cost[v] + e.cost; dfs(e.to, v, d+1); } } void init(int root=0) { depth.resize(n, 0); cost.resize(n, 0); parent.resize(20, vector(n, 0)); dfs(root); rep(k, 19) { rep(v, n) { if (parent[k][v] < 0) parent[k+1][v] = -1; else parent[k+1][v] = parent[k][parent[k][v]]; } } } int lca(int u, int v) { if (depth[u] > depth[v]) swap(u, v); rep(k, 20) { if ((depth[v] - depth[u]) & (1 << k)) v = parent[k][v]; } if (u == v) return u; for (int k = 19; k >= 0; k--) { if (parent[k][u] == parent[k][v]) continue; u = parent[k][u]; v = parent[k][v]; } return parent[0][v]; } int main() { cin >> n; rep(i, n-1) { int u, v, w; cin >> u >> v >> w; edge e1 = {v, w}; g[u].push_back(e1); edge e2 = {u, w}; g[v].push_back(e2); } init(); int q; cin >> q; rep(i, q) { int x, y, z; cin >> x >> y >> z; int xy = lca(x, y); int yz = lca(y, z); int zx = lca(z, x); cout << (cost[x] + cost[y] + cost[z] - (cost[xy] + cost[yz] + cost[zx])) << endl; } return 0; }