#include using namespace std; #define rep(i,n) for(int i = 0; i < (n);i++) #define sz(x) int(x.size()) typedef long long ll; typedef long double ld; typedef pair P; typedef pair PL; constexpr int INF = 2e9; struct LowestCommonAncestor { int n, h; vector> par; vector>> g; vector dep; vector weight; LowestCommonAncestor(int sz) { n = sz; h = 1; g.resize(n), dep.resize(n), weight.resize(n); while ((1 << h) <= n) h++; par.assign(h, vector(n, -1)); } void add_edge(int u, int v, int w = 0) { g[u].emplace_back(make_pair(v, w)); g[v].emplace_back(make_pair(u, w)); } void dfs(int u, int p, int depth) { par[0][u] = p; dep[u] = depth; if (p == -1) weight[u] = 0; for (auto v : g[u]) { if (v.first == p) continue; weight[v.first] = weight[u] + v.second; dfs(v.first, u, depth + 1); } } void build(int root = 0) { dfs(root, -1, 0); for (int i = 0; i + 1 < h; i++) { for (int j = 0; j < n; j++) { if (par[i][j] != -1) par[i+1][j] = par[i][par[i][j]]; else par[i+1][j] = -1; } } } int query(int u, int v) { if (dep[u] > dep[v]) swap(u, v); for (int i = h-1; i >= 0; i--) { if ( ((dep[v] - dep[u]) >> i) & 1) v = par[i][v]; } if (u == v) return u; for (int i = h-1; i >= 0; i--) { if (par[i][u] != par[i][v]) { u = par[i][u]; v = par[i][v]; } } return par[0][u]; } int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[query(u, v)]; } int weight_sum(int u, int v) { return weight[u] + weight[v] - 2 * weight[query(u, v)]; } }; int main() { int n; scanf("%d", &n); LowestCommonAncestor lca(n); rep(i,n-1) { int a, b, c; scanf("%d %d %d", &a, &b, &c); a--; b--; lca.add_edge(a, b, c); } lca.build(); int q; scanf("%d", &q); while (q--) { int s, t; scanf("%d %d", &s, &t); s--; t--; printf("%d\n", lca.weight_sum(s, t)); } return 0; }