#include using namespace std::literals::string_literals; using i64 = long long; using std::cout; using std::endl; using std::cin; template std::vector make_v(size_t a){return std::vector(a);} template auto make_v(size_t a,Ts... ts){ return std::vector(ts...))>(a,make_v(ts...)); } struct LowestCommonAncestor{ std::vector> g, parent; std::vector depth; bool built = false; int N; const int logN = 20; LowestCommonAncestor(int n) : N(n) { g.resize(N); parent.resize(logN, std::vector(N)); depth.resize(N); } void add_edge(int u, int v){ g[u].push_back(v); g[v].push_back(u); } void dfs(int v, int par = -1, int d = 0){ parent[0][v] = par; depth[v] = d; for(auto e:g[v]){ if(e == par) continue; dfs(e, v, d + 1); } } void build(){ dfs(0); for(int k = 0; k < logN - 1; k++){ for(int i = 0; i < N; i++){ if(parent[k][i] < 0) parent[k + 1][i] = -1; else parent[k + 1][i] = parent[k][parent[k][i]]; } } } int get(int u, int v){ if(!built) build(); built = true; if(depth[u] > depth[v]) std::swap(u, v); for(int k = 0; k < logN; k++){ if(((depth[v] - depth[u]) >> k) & 1){ v = parent[k][v]; } } if(u == v) return u; for(int k = logN - 1; k >= 0; k--){ if(parent[k][u] == parent[k][v]) continue; u = parent[k][u]; v = parent[k][v]; } return parent[0][u]; } }; int main() { // input int n; scanf("%d", &n); assert(1 <= n and n <= (int)1e5); LowestCommonAncestor lca(n); std::vector>> g(n); for(int i = 0; i < n - 1; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); assert(0 <= a and a < n); assert(0 <= b and b < n); assert(1 <= c and c <= (int)1e9); g[a].push_back({b, c}); g[b].push_back({a, c}); lca.add_edge(a, b); } lca.build(); // solve std::vector dist(n); auto dfs = [&](auto && dfs, int v, int par) -> void { for(auto e: g[v]) { if(e.first == par) continue; dist[e.first] = dist[v] + e.second; dfs(dfs, e.first, v); } }; dfs(dfs, 0, -1); auto calc = [&lca, dist](const int & x, const int & y) -> i64 { return dist[x] + dist[y] - 2LL * dist[lca.get(x, y)]; }; int q; scanf("%d", &q); while(q--) { int x, y, z; scanf("%d%d%d", &x, &y, &z); assert(0 <= x and x < n); assert(0 <= y and y < n); assert(0 <= z and z < n); printf("%lld\n", (calc(x, y) + calc(y, z) + calc(z, x)) >> 1); } return 0; }