#include using namespace std; typedef long long ll; typedef pair l_l; typedef pair i_i; template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) //const ll mod = 1000000007; typedef vector> Graph; template< typename G > 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]; } }; vector pathes[100500]; ll cost[100500]; void DFS(int now, int from) { for(l_l a : pathes[now]) { if(a.first == from) continue; cost[a.first] = cost[now] + a.second; DFS(a.first, now); } } ll f(ll a, ll b, ll c) { return cost[a] + cost[b] - 2 * cost[c]; } int main() { //cout.precision(10); cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; Graph G(N); for(int i = 0; i <= N - 2; i++) { ll u, v, w; cin >> u >> v >> w; G[u].push_back(v); G[v].push_back(u); pathes[u].push_back({v, w}); pathes[v].push_back({u, w}); } DoublingLowestCommonAncestor LCA(G); LCA.build(); DFS(0, -1); ll Q; cin >> Q; while(Q--) { ll x, y, z; cin >> x >> y >> z; ll ans = 0; ans += f(x, y, LCA.query(x, y)); ans += f(z, y, LCA.query(z, y)); ans += f(x, z, LCA.query(x, z)); ans /= 2; cout << ans << endl; } return 0; }