#include using namespace std; const int LOG = 17; int main(){ int N; cin >> N; vector>> E(N); for (int i = 0; i < N - 1; i++){ int a, b, c; cin >> a >> b >> c; a--; b--; E[a].push_back(make_pair(c, b)); E[b].push_back(make_pair(c, a)); } vector p(N, -1); vector> c(N); vector d1(N, 0); vector d2(N, 0); queue q; q.push(0); while (!q.empty()){ int v = q.front(); q.pop(); for (auto P : E[v]){ int w = P.second; if (w != p[v]){ p[w] = v; c[v].push_back(w); d1[w] = d1[v] + 1; d2[w] = d2[v] + P.first; q.push(w); } } } vector> pp(LOG, vector(N, -1)); pp[0] = p; for (int i = 0; i < LOG - 1; i++){ for (int j = 0; j < N; j++){ if (pp[i][j] != -1){ pp[i + 1][j] = pp[i][pp[i][j]]; } } } int Q; cin >> Q; for (int i = 0; i < Q; i++){ int s, t; cin >> s >> t; s--; t--; int ans = 0; if (d1[s] > d1[t]){ swap(s, t); } for (int j = 0; j < LOG; j++){ if (((d1[t] - d1[s]) >> j & 1) == 1){ ans += d2[t] - d2[pp[j][t]]; t = pp[j][t]; } } if (s != t){ for (int j = LOG - 1; j >= 0; j--){ if (pp[j][s] != pp[j][t]){ ans += d2[s] - d2[pp[j][s]]; ans += d2[t] - d2[pp[j][t]]; s = pp[j][s]; t = pp[j][t]; } } ans += d2[s] - d2[p[s]]; ans += d2[t] - d2[p[t]]; } cout << ans << endl; } }