#include #include #define llint long long using namespace std; struct edge{ llint to, cost; edge(){} edge(llint a, llint b){ to = a, cost = b; } }; llint n, Q; vector G[100005]; int Prev[100005][17]; int depth[100005]; llint dist[100005]; int getLCA(int u, int v){ int x = u, y = v; if(depth[y] > depth[x]) swap(x, y); for(int i = 16; i >= 0; i--){ if(depth[x] - (1<= depth[y]) x = Prev[x][i]; } if(x == y) return x; for(int i = 16; i >= 0; i--){ if(Prev[x][i] != Prev[y][i]){ x = Prev[x][i]; y = Prev[y][i]; } } x = Prev[x][0]; return x; } void dfs(int v, int p, int d, llint dst) { Prev[v][0] = p, depth[v] = d, dist[v] = dst; for(int i = 0; i < G[v].size(); i++){ if(G[v][i].to == p) continue; dfs(G[v][i].to, v, d+1, dst + G[v][i].cost); } } llint calc(llint s, llint t) { llint lca = getLCA(s, t); return dist[s] + dist[t] - 2 * dist[lca]; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n; llint u, v, w; for(int i = 1; i <= n-1; i++){ cin >> u >> v >> w; u++, v++; G[u].push_back(edge(v, w)); G[v].push_back(edge(u, w)); } dfs(1, -1, 0, 0); for(int i = 1; i < 16; i++){ for(int j = 1; j <= n; j++){ Prev[j][i] = Prev[Prev[j][i-1]][i-1]; } } cin >> Q; llint x, y, z; for(int q = 0; q < Q; q++){ cin >> x >> y >> z; x++, y++, z++; llint ans = calc(x, y) + calc(y, z) + calc(z, x); cout << ans / 2 << "\n"; } flush(cout); return 0; }