/*** author: yuji9511 ***/ #include using namespace std; typedef long long ll; typedef pair lpair; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i=(m);i<(n);i++) #define rrep(i,m,n) for(ll i=(m);i>=(n);i--) #define printa(x,n) for(ll i=0;i void print(H&& h, T&&... t){cout<(t)...);} struct Tree { private: ll N, logN; vector edge[200010]; ll depth[200010] = {}; ll sum[200010] = {}; ll parent[200010] = {}; ll next_val[20][200010] = {}; public: Tree(){ } void init(ll n){ N = n; logN = floor(log2(N)) + 1; } ll getDepth(ll x) { return depth[x]; } ll getSum(ll x) { return sum[x]; } void add(ll x, ll y, ll z){ edge[x].push_back({y,z}); } void prepare(){ dfs_parent(); dfs_sum(); makeDoubling(); } void dfs_parent(ll cur = 0, ll par = -1, ll d = 0){ depth[cur] = d; parent[cur] = par; for(auto &e: edge[cur]){ if(e.first == par) continue; dfs_parent(e.first, cur, d+1); } } void dfs_sum(ll cur = 0, ll par = -1, ll d = 0){ if(par != -1) sum[cur] = sum[par] + d; for(auto &e: edge[cur]){ if(e.first == par) continue; dfs_sum(e.first, cur, e.second); } } void makeDoubling(){ rep(i,0,N) next_val[0][i] = parent[i]; rep(k,0,logN){ rep(i,0,N){ if(next_val[k][i] == -1) next_val[k+1][i] = -1; else next_val[k+1][i] = next_val[k][next_val[k][i]]; } } } ll getParent(ll cur, ll num) { ll p = cur; rrep(k,logN-1, 0){ if(p == -1) break; if((num >> k) & 1) p = next_val[k][p]; } return p; } ll getLCA(ll va, ll vb){ if(depth[va] > depth[vb]){ va = getParent(va, depth[va] - depth[vb]); }else if(depth[va] < depth[vb]){ vb = getParent(vb, depth[vb] - depth[va]); } ll lv = -1, rv = N+1; while(rv - lv > 1){ ll mid = (rv + lv) / 2; ll ta = getParent(va, mid); ll tb = getParent(vb, mid); if(ta == -1 || tb == -1){ rv = mid; }else if(ta != tb){ lv = mid; }else{ rv = mid; } } return getParent(va, rv); } } tree; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; tree.init(N); rep(i,0,N-1){ ll u,v,w; cin >> u >> v >> w; tree.add(u,v,w); tree.add(v,u,w); } tree.prepare(); ll Q; cin >> Q; while(Q--){ ll x,y,z; cin >> x >> y >> z; ll ans = tree.getSum(x) + tree.getSum(y) + tree.getSum(z); ll v1 = tree.getLCA(x,y); ll v2 = tree.getLCA(y,z); ll v3 = tree.getLCA(x,z); ans -= (tree.getSum(v1) + tree.getSum(v2) + tree.getSum(v3)); print(ans); } }