/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author */ #include #include #ifndef SOLUTION_COMMON_H #include using namespace std; using ll = long long; using PI = pair; template using V = vector; using VI = V; #define _1 first #define _2 second #ifdef MY_DEBUG # define DEBUG(x) x #else # define DEBUG(x) #endif template inline void debug(T &A) { DEBUG( for (const auto &a : A) { cerr << a << " "; } cerr << '\n'; ) } template inline void debug_with_format(T &A, Func f) { DEBUG( for (const auto &a : A) { cerr << f(a) << " "; } cerr << '\n'; ) } template inline void debug_dim2(T &A) { DEBUG( for (const auto &as : A) { debug(as); } ) } template inline void debug(const char *format, Args const &... args) { DEBUG( fprintf(stderr, format, args ...); cerr << '\n'; ) } template string format(const string &fmt, Args ... args) { size_t len = snprintf(nullptr, 0, fmt.c_str(), args ...); vector buf(len + 1); snprintf(&buf[0], len + 1, fmt.c_str(), args ...); return string(&buf[0], &buf[0] + len); } template string fmtP(pair a) { stringstream ss; ss << "(" << a._1 << "," << a._2 << ")"; return ss.str(); } #define SOLUTION_COMMON_H #endif //SOLUTION_COMMON_H const int MOD = 1000000007; class LCA { public: int n; V anc; VI depth; V co; int k; LCA(V> &g, int k, int rt = 0): n((int)g.size()), anc(k, VI(n)), depth(n, -1), co(n), k(k) { function initParent = [&](int v, int p, int c) { debug("%d %d", v, p); anc[0][v] = p; depth[v] = p == -1 ? 0 : depth[p] + 1; co[v] = p == -1 ? 0 : co[p] + c; for (const auto &u : g[v]) { if (depth[u._1] == -1) initParent(u._1, v, u._2); } }; initParent(rt, -1, 0); for (int kk = 1; kk < k; ++kk) { for (int i = 0; i < n; ++i) { if (anc[kk - 1][i] == -1) { anc[kk][i] = -1; } else { anc[kk][i] = anc[kk - 1][anc[kk - 1][i]]; } } } } int get_anc(int v, int dist) { int i = 0; while(dist > 0) { if (dist >> i & 1) { dist -= 1 << i; v = anc[i][v]; } i++; } return v; } int operator()(int v, int u) { int d = min(depth[v], depth[u]); v = get_anc(v, depth[v] - d); u = get_anc(u, depth[u] - d); if (v == u) return v; function dfs = [&](int v, int u, int s) { for (int kk = s; kk >= 0; --kk) { if (anc[kk][u] != anc[kk][v]) { return dfs(anc[kk][u], anc[kk][v], kk - 1); } } return anc[0][v]; }; return dfs(v, u, k - 1); } }; class A { public: void solve(std::istream& in, std::ostream& out) { int n; in >> n; V> g(n); for (int i = 0; i < n - 1; ++i) { int a, b, c; in >> a >> b >> c; g[a].emplace_back(b, c); g[b].emplace_back(a, c); } LCA lca(g, 17); int q; in >> q; debug("q:%d", q); debug(lca.co); auto path = [&](int v, int u) { int anc = lca(u, v); return lca.co[v] + lca.co[u] - 2 * lca.co[anc]; }; for (int i = 0; i < q; ++i) { int x, y, z; in >> x >> y >> z; auto v1 = path(x, y) + path(z, lca(x, y)); auto v2 = path(y, z) + path(x, lca(y, z)); auto v3 = path(x, z) + path(y, lca(y, z)); auto ans = min(v1, min(v2, v3)); out << ans << '\n'; } } }; int main() { A solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }