#include using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for(int64 i = 0;i < (n);i++) #define FOR(i, a, b) for(int64 i = (a);i < (b);i++) #define all(x) x.begin(),x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair; using PLL = pair; const double eps = 1e-10; templateinline void chmin(A &a, B b){if(a > b) a = b;} templateinline void chmax(A &a, B b){if(a < b) a = b;} template vector make_v(size_t a){return vector(a);} template auto make_v(size_t a,Ts... ts){ return vector(ts...))>(a,make_v(ts...)); } template typename enable_if::value!=0>::type fill_v(U &u,const V... v){u=U(v...);} template typename enable_if::value==0>::type fill_v(U &u,const V... v){ for(auto &e:u) fill_v(e,v...); } int64 N; vector G[112345]; vector dep, d_sum; vector> table; void dfs(int64 v, int64 p, int64 d = 0, int64 w = 0) { dep[v] = d; d_sum[v] = d_sum[p] + w; table[v][0] = p; REP(i, G[v].size()) { if (G[v][i].sc == p) continue; dfs(G[v][i].sc, v, d+1, G[v][i].fs); } } void build() { dep.resize(N, 0); d_sum.resize(N, 0); table.resize(N, vector(20, 0)); dfs(0, 0); FOR(i, 1, 20) { REP(j, N) { table[j][i] = table[table[j][i-1]][i-1]; } } } int64 lca(int64 u, int64 v) { if (dep[u] < dep[v]) swap(u, v); REP(i, 20) { if ((dep[u]-dep[v]) >> i & 1) { u = table[u][i]; } } if (u == v) return u; for (int64 i = 19; i >= 0; i--) { if (table[u][i] != table[v][i]) { u = table[u][i]; v = table[v][i]; } } return table[u][0]; } int main(void) { cin >> N; REP(i, N-1) { int64 u, v, w; cin >> u >> v >> w; G[u].emplace_back(w, v); G[v].emplace_back(w, u); } build(); int64 Q; cin >> Q; REP(i, Q) { vector vv(3); REP(j, 3) cin >> vv[j]; int64 now = -1; int64 res = 0; REP(j, 3) { FOR(k, j+1, 3) { if (now == -1 || dep[now] < dep[lca(vv[j], vv[k])]) { now = lca(vv[j], vv[k]); // cout << vv[j] << " " << vv[k] << " " << vv[3-j-k] << " " << now; res = d_sum[vv[j]] + d_sum[vv[k]] - 2 * d_sum[now]; // cout << " " << res << " "; res += d_sum[now] + d_sum[vv[3-j-k]] - 2 * d_sum[lca(now, vv[3-j-k])]; // cout << res << endl; } } } cout << res << endl; } }