#include #include #include #define int long long using namespace std; int n; vector et[100000]; vector ec[100000]; int q; int x[100000], y[100000], z[100000]; int parent[100000]; int depth[100000]; int dist[100000]; void dfs(int p, int v, int dep, int dis) { int i; depth[v] = dep; parent[v] = p; dist[v] = dis; for (i = 0; i < et[v].size(); i++) { int nv = et[v][i]; if (nv == p) continue; dfs(v, nv, dep + 1, dis + ec[v][i]); } } int parents[20][100000]; int lca(int u, int v) { if (depth[u] < depth[v]) { swap(u, v); } int i; int diff = depth[u] - depth[v]; for (i = 0; i < 20; i++) { if ((diff >> i) % 2 == 1) { u = parents[i][u]; } } for (i = 19; i >= 0; i--) { if (parents[i][u] != parents[i][v]) { u = parents[u][i]; v = parents[v][i]; } } if (u == v) return u; return parent[u]; } int distance(int u, int v) { return dist[u] + dist[v] - 2 * dist[lca(u, v)]; } signed main() { int i, j; cin >> n; for (i = 0; i < n - 1; i++) { int u, v, w; cin >> u >> v >> w; et[u].push_back(v); ec[u].push_back(w); et[v].push_back(u); ec[v].push_back(w); } cin >> q; for (i = 0; i < q; i++) { cin >> x[i] >> y[i] >> z[i]; } dfs(0, 0, 0, 0); for (i = 0; i < n; i++) { parents[0][i] = parent[i]; } for (i = 0; i < 19; i++) { for (j = 0; j < n; j++) { parents[i + 1][j] = parents[i][parents[i][j]]; } } lca(3, 4); for (i = 0; i < q; i++) { int distXY = distance(x[i], y[i]); int distYZ = distance(y[i], z[i]); int distZX = distance(z[i], x[i]); cout << (distXY + distYZ + distZX) / 2 << endl; } return 0; }