#include using namespace std; typedef long long ll; typedef long double ld; #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (ll i = m; i < n; ++i) #define FORR(i, m, n) for (ll i = m; i >= n; --i) #define ALL(v) (v).begin(),(v).end() templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b struct DoublingLowestCommonAncestor { const int LOG; vector< int > dep; const G &g; vector< vector< int > > table; DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) { table.assign(LOG, vector< int >(g.size(), -1)); } void dfs(int idx, int par, int d) { table[0][idx] = par; dep[idx] = d; for(auto &to : g[idx]) { if(to != par) dfs(to, idx, d + 1); } } void build() { dfs(0, -1, 0); for(int k = 0; k + 1 < LOG; k++) { for(int i = 0; i < table[k].size(); i++) { if(table[k][i] == -1) table[k + 1][i] = -1; else table[k + 1][i] = table[k][table[k][i]]; } } } int query(int u, int v) { if(dep[u] > dep[v]) swap(u, v); for(int i = LOG - 1; i >= 0; i--) { if(((dep[v] - dep[u]) >> i) & 1) v = table[i][v]; } if(u == v) return u; for(int i = LOG - 1; i >= 0; i--) { if(table[i][u] != table[i][v]) { u = table[i][u]; v = table[i][v]; } } return table[0][u]; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n;cin >> n; vector>> e(n); vector> g(n); REP(i,n-1){ int u,v,w;cin >> u >> v >> w; e[u].push_back({v,w}); e[v].push_back({u,w}); g[u].push_back(v); g[v].push_back(u); } vector d(n,-1); d[0]=0; stack st; st.push(0); while(!st.empty()){ int p=st.top(); st.pop(); for(auto x:e[p]){ if(d[x.first]==-1){ d[x.first]=d[p]+x.second; st.push(x.first); } } } DoublingLowestCommonAncestor>> lca(g); lca.build(); int q;cin >> q; while(q--){ int x,y,z;cin >> x >> y >> z; cout << d[x]+d[y]+d[z]-d[lca.query(x,y)]-d[lca.query(y,z)]-d[lca.query(z,x)] << endl; } }