#include using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 1000000007; constexpr bool debug = false; //---------------------------------// struct HeavyLightDecomposition { using size_type = std::uint_fast32_t; using Graph = std::vector>; private: size_type bf_n; // グラフの頂点数 std::vector par_; // [v] := 頂点 v の親の頂点番号(存在しなければ自分自身) std::vector sub_size_; // [v] := 頂点 v を根とする部分木のサイズ std::vector depth_; // [v] := 頂点 v の元のグラフでの深さ std::vector tree_id_; // [v] := 頂点 v が属する木の id std::vector roots_; // [i] := i 番目の木の root std::vector heavy_map_; // [v] := 頂点 v が属する heavy-path id std::vector head_; // [i] := heavy-path i の最も根に近い頂点番号 std::vector heavy_size_ ; // [i] := heavy-path i に属する頂点の個数 std::vector heavy_depth_; // [i] := heavy-path i から根までに通る light-edge の個数 // euler-tour std::vector in_; // [v] := 頂点 v の EulerTour 順序(同一 heavy-path 内では連続) std::vector out_; // [v] := 頂点 v から出るときの EulerTour 順序 std::vector euler_map_; // [i] := EulerTour 順序が i であるような頂点 // heavy-path doubling std::vector> par_dblng_; // [k][i] := heavy-path i から 2^k 回 light-edge を上った先の頂点 public: HeavyLightDecomposition(const Graph & g, bool use_lca = false) : HeavyLightDecomposition(g, g.size(), use_lca) {} HeavyLightDecomposition(const Graph & g, size_type root, bool use_lca) : bf_n(g.size()) { par_.resize(bf_size()); sub_size_.resize(bf_size()); depth_.resize(bf_size()); tree_id_.assign(bf_size(), bf_size()); std::vector next(bf_size()); // [v] := 頂点 v と同一 heavy-path 内で v より 1 つ葉側の頂点(存在しなければ自分自身) for (size_type i = 0; i < bf_size(); ++i) { if (tree_id_[i] != bf_size()) continue; if (root != bf_size() && i != root) continue; std::stack> stk; par_[i] = i; depth_[i] = 0; tree_id_[i] = roots_.size(); stk.emplace(i, 0); while (!stk.empty()) { const size_type u = stk.top().first, i = stk.top().second; stk.pop(); if (i < g[u].size()) { stk.emplace(u, i + 1); const size_type v = g[u][i]; if (v == par_[u]) continue; par_[v] = u; depth_[v] = depth_[u] + 1; tree_id_[v] = roots_.size(); stk.emplace(v, 0); } else { size_type mx = 0; next[u] = u; sub_size_[u] = 1; for (size_type v : g[u]) { if (v == par_[u]) continue; sub_size_[u] += sub_size_[v]; if (mx < sub_size_[v]) { mx = sub_size_[v]; next[u] = v; } } } } roots_.emplace_back(i); } heavy_map_.resize(bf_size()); in_.resize(bf_size()); out_.resize(bf_size()); euler_map_.reserve(bf_size()); for (size_type root : roots_) { std::stack> stk; heavy_map_[root] = head_.size(); head_.emplace_back(root); heavy_size_.emplace_back(1); heavy_depth_.emplace_back(0); stk.emplace(root, 0); while (!stk.empty()) { const size_type u = stk.top().first, i = stk.top().second; stk.pop(); if (i < g[u].size()) { stk.emplace(u, i + 1); const size_type v = g[u][i]; if (v != par_[u] && v != next[u]) { heavy_map_[v] = head_.size(); head_.emplace_back(v); heavy_size_.emplace_back(1); heavy_depth_.emplace_back(heavy_depth_[heavy_map_[u]] + 1); stk.emplace(v, 0); } } if (i == 0) { in_[u] = euler_map_.size(); euler_map_.emplace_back(u); const size_type v = next[u]; if (v != u) { heavy_map_[v] = heavy_map_[u]; ++heavy_size_[heavy_map_[u]]; stk.emplace(v, 0); } } if (i == g[u].size()) out_[u] = euler_map_.size(); } } if (!use_lca) return; size_type max_depth = *std::max_element(begin(heavy_depth_), end(heavy_depth_)); size_type lglg_n = 0; while ((1 << lglg_n) < max_depth) ++lglg_n; par_dblng_.assign(lglg_n + 1, std::vector(af_size())); for (size_type i = 0; i < af_size(); ++i) par_dblng_[0][i] = par_[head_[i]]; for (size_type i = 0; i < lglg_n; ++i) { for (size_type j = 0; j < af_size(); ++j) { par_dblng_[i + 1][j] = par_dblng_[i][heavy_map_[par_dblng_[i][j]]]; } } } size_type bf_size() const noexcept { return bf_n; } size_type af_size() const noexcept { return head_.size(); } size_type par(size_type v) const { assert(v < bf_size()); return par_[v]; } size_type sub_size(size_type v) const { assert(v < bf_size()); return sub_size_[v]; } size_type depth(size_type v) const { assert(v < bf_size()); return depth_[v]; } size_type tree_id(size_type v) const { assert(v < bf_size()); return tree_id_[v]; } size_type tree_cnt() const noexcept { return roots_.size(); } const std::vector & trees() const noexcept { return roots_; } size_type heavy_map(size_type v) const { assert(v < bf_size()); return heavy_map_[v]; } size_type head(size_type k) const { assert(k < af_size()); return head_[k]; } size_type heavy_size(size_type k) const { assert(k < af_size()); return heavy_size_[k]; } size_type heavy_depth(size_type k) const { assert(k < af_size()); return heavy_depth_[k]; } size_type in(size_type v) const { assert(v < bf_size()); return in_[v]; } size_type out(size_type v) const { assert(v < bf_size()); return out_[v]; } size_type euler_map(size_type k) const { assert(k < bf_size()); return euler_map_[k]; } const std::vector> & par_dblng() const { assert(!par_dblng_.empty()); return par_dblng_; } std::pair get_lca_path(size_type x, size_type y) const { assert(!par_dblng_.empty()); assert(x < bf_size()); assert(y < bf_size()); assert(tree_id_[x] == tree_id_[y]); if (heavy_map_[x] == heavy_map_[y]) return {x, y}; bool isswap = heavy_depth_[heavy_map_[x]] < heavy_depth_[heavy_map_[y]]; if (isswap) std::swap(x, y); const size_type diff = heavy_depth_[heavy_map_[x]] - heavy_depth_[heavy_map_[y]]; for (size_type i = par_dblng_.size(); i > 0; --i) { if (diff >> (i - 1) & 1) x = par_dblng_[i - 1][heavy_map_[x]]; } if (heavy_map_[x] == heavy_map_[y]) return isswap ? std::make_pair(y, x) : std::make_pair(x, y); for (size_type i = par_dblng_.size(); i > 0; --i) { const size_type p1 = par_dblng_[i - 1][heavy_map_[x]], p2 = par_dblng_[i - 1][heavy_map_[y]]; if (heavy_map_[p1] != heavy_map_[p2]) x = p1, y = p2; } x = par_dblng_[0][heavy_map_[x]]; y = par_dblng_[0][heavy_map_[y]]; return isswap ? std::make_pair(y, x) : std::make_pair(x, y); } size_type get_lca(size_type x, size_type y) { assert(!par_dblng_.empty()); assert(x < bf_size()); assert(y < bf_size()); std::pair res = get_lca_path(x, y); return in_[res.first] < in_[res.second] ? res.first : res.second; } }; int main() { using HLD = HeavyLightDecomposition; int N; cin >> N; HLD::Graph hlg(N); vector> g(N); REP(i, N - 1) { int u, v, w; scanf("%d %d %d", &u, &v, &w); hlg[u].emplace_back(v); hlg[v].emplace_back(u); g[u].emplace_back(v, w); g[v].emplace_back(u, w); } HLD hld(hlg, true); vector dist(N); auto dfs = [&](auto self, int u, int p) -> void { for (auto [v, d] : g[u]) { if (v == p) continue; dist[v] = dist[u] + d; self(self, v, u); } }; dfs(dfs, 0, -1); int Q; cin >> Q; while (Q--) { int x, y, z; scanf("%d %d %d", &x, &y, &z); const int xy = hld.get_lca(x, y); ll ans = dist[x] + dist[y] - 2 * dist[xy]; if (xy == x) swap(x, y); const int xz = hld.get_lca(x, z), yz = hld.get_lca(y, z); // y -> x if (xy == y) { if (xz == x) ans += dist[z] - dist[x]; else if (yz == y) ans += dist[z] - dist[xz]; else if (yz == z) ans += dist[y] - dist[z]; else ans += dist[z] + dist[y] - 2 * dist[yz]; } else { if (xz == z && yz == z) ans += dist[xy] - dist[z]; else if (xz == x) ans += dist[z] - dist[x]; else if (yz == y) ans += dist[z] - dist[y]; else if (hld.get_lca(xz, yz) == xy) { if (xz != xy) ans += dist[z] - dist[xz]; else ans += dist[z] - dist[yz]; } else if (xz == z) ans += dist[xy] - dist[z]; else ans += dist[z] + dist[xy] - 2 * dist[xz]; } printf("%lld\n", ans); } }