#include using namespace std; using LL = long long; using ULL = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) const int xN = 100000; struct Edge { int to; LL dist; }; int N; vector E[xN]; int P[xN], D[xN]; int lcaP[20][xN]; LL lcaD[xN]; void dfs(int p = 0) { for (Edge& e : E[p]) { if (P[p] == e.to) continue; D[e.to] = D[p] + e.dist; lcaD[e.to] = lcaD[p] + 1; P[e.to] = p; dfs(e.to); } } void init() { rep(i, N) P[i] = -1; rep(i, N) D[i] = 0; dfs(0); rep(i, N) lcaP[0][i] = P[i]; lcaP[0][0] = 0; rep(d, 19) rep(i, N) lcaP[d + 1][i] = lcaP[d][lcaP[d][i]]; } int LCA(int u, int v) { if (lcaD[u] > lcaD[v]) swap(u, v); for (int i = 19; i >= 0; i--) if (lcaD[u] + (1 << i) <= lcaD[v]) v = lcaP[i][v]; if (u == v) return u; for (int i = 19; i >= 0; i--) if (lcaP[i][u] != lcaP[i][v]) { u = lcaP[i][u]; v = lcaP[i][v]; } return lcaP[0][u]; } LL distance(int u, int v) { int g = LCA(u, v); return D[u] + D[v] - 2 * D[g]; } int main() { cin >> N; rep(i, N - 1) { int u, v, c; cin >> u >> v >> c; E[u].push_back({ v,c }); E[v].push_back({ u,c }); } init(); int Q; cin >> Q; while (Q--) { int x, y, z; cin >> x >> y >> z; LL ans = distance(x, y) + distance(y, z) + distance(z, x); ans /= 2; cout << ans << endl; } return 0; }