#include #include #include using namespace std; typedef long long LL; const int N = 100010, M = 2 * N, L = 20; LL dist[N]; int n, q, h[N], e[M], ne[M], w[M], idx, dep[N], anc[N][L]; void Add(int x, int y, int z) { e[idx] = y, w[idx] = z, ne[idx] = h[x], h[x] = idx++; } void DFS(int u, int p) { dep[u] = dep[p] + 1, anc[u][0] = p; for (int i = 1; i < L; ++i) anc[u][i] = anc[anc[u][i - 1]][i - 1]; for (int i = h[u]; i != -1; i = ne[i]) { int v = e[i]; if (v == p) continue; dist[v] = dist[u] + w[i]; DFS(v, u); } } int LCA(int u, int v) { if (dep[u] < dep[v]) swap(u, v); int d = dep[u] - dep[v]; for (int i = 0; i < L; ++i) { if (d >> i & 1) { u = anc[u][i]; } } if (u == v) return u; for (int i = L - 1; i >= 0; --i) { if (anc[u][i] != anc[v][i]) { u = anc[u][i]; v = anc[v][i]; } } return anc[u][0]; } LL Dist(int x, int y) { int lca = LCA(x, y); return dist[x] + dist[y] - 2 * dist[lca]; } int main() { // freopen("lucky.in", "r", stdin); // freopen("lucky.out", "w", stdout); scanf("%d", &n); memset(h, -1, sizeof(h)); for (int i = 1, x, y, z; i <= n - 1; ++i) { scanf("%d%d%d", &x, &y, &z); ++x, ++y; Add(x, y, z), Add(y, x, z); } DFS(1, 0); scanf("%d", &q); while (q--) { int x, y, z; scanf("%d%d%d", &x, &y, &z); ++x, ++y, ++z; printf("%lld\n", (Dist(x, y) + Dist(y, z) + Dist(z, x)) / 2); } return 0; }