#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...)); } class lowest_common_ancestor { std::vector> g, parent; std::vector depth; const size_t logN = 20; private: void dfs(size_t v, int par = -1, size_t d = 0) { parent[0][v] = par; depth[v] = d; for(auto e:g[v]) { if(e == par) continue; dfs(e, v, d + 1); } } public: lowest_common_ancestor() {} lowest_common_ancestor(int n) : g(n), depth(n) { parent.assign(logN, std::vector(n, -1)); } void add_edge(const size_t & u, const size_t & v) { g[u].push_back(v); g[v].push_back(u); } void build() { dfs(0); for(int k = 0; k < logN - 1; k++) { for(int i = 0; i < g.size(); i++) { if(parent[k][i] == -1) parent[k + 1][i] = -1; else parent[k + 1][i] = parent[k][parent[k][i]]; } } } const size_t query(size_t u, size_t v) { 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); lowest_common_ancestor 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.query(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; } // Θ((N + Q)logN)