#include using namespace std; typedef long long ll; const int nax = 1e5 + 7; vector>edge[nax]; vector>dp(nax, vector(22)); vectortin(nax, 0); vectortout(nax, 0); vectordp1(nax, 0); int timer; void dfs(int child, int par) { tin[child] = timer++; dp[child][0] = par; for (int i = 1; i < 20; i++) { dp[child][i] = dp[dp[child][i - 1]][i - 1]; } for (auto k : edge[child]) { if (k.first != par) { dp1[k.first] = k.second + dp1[child]; dfs(k.first, child); } } tout[child] = timer++; } bool isancesstor(int u, int v) { return (tin[v] >= tin[u] && tout[u] >= tout[v]); } int lca(int u, int v) { if (isancesstor(u, v))return u; if (isancesstor(v, u))return v; for (int i = 19; i >= 0; i--) { if (!isancesstor(dp[u][i], v)) { u = dp[u][i]; } } return dp[u][0]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < (n - 1); i++) { int x, y, val; cin >> x >> y >> val; edge[x].push_back({y, val}); edge[y].push_back({x, val}); } dfs(1, 0); tout[0] = 1e9; int q; cin >> q; // for (int i = 1; i <= n; i++) { // cout << dp1[i] << " "; // } // cout << endl; for (int i = 0; i < q; i++) { int x, y; cin >> x >> y; int get_lca = lca(x, y); cout << dp1[x] + dp1[y] - 2 * dp1[get_lca] << endl; } }