#include #define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++) #define rep(i, begin, end) for(int i = int(begin); i < int(end); i++) #define all(v) v.begin(), v.end() #define rand(l, r) std::uniform_int_distribution<>(l, r)(mt) using lint = long long; auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;}; auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;}; void debug_impl() { std::cerr << std::endl; } template void debug_impl(Head head, Tail... tail){ std::cerr << " " << head; debug_impl(tail...); } #ifndef STOPIT #define debug(...)\ std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\ debug_impl(__VA_ARGS__);\ std::cerr << std::noboolalpha; #else #define debug 0; #endif template< typename F > class fixed_point : F { public: explicit constexpr fixed_point (F&& f) noexcept : F(std::forward< F >(f)) {} template< typename ... Args > constexpr decltype(auto) operator()(Args&& ... args) const { return F::operator()(*this, std::forward< Args >(args)...); } }; template< typename F > static inline constexpr decltype(auto) fix (F&& f) noexcept { return fixed_point< F >{std::forward< F >(f)}; } // Class for calculating LCA. // Dependent libraries: Combinator. template class lowest_common_ancestor { struct edge { size_t to; T cost; edge(size_t to, T cost) : to(to), cost(cost){} }; size_t n, lg; std::vector> graph; std::vector depth; std::vector weighted_depth; std::vector> prt; public: using cost_type = T; lowest_common_ancestor(size_t n) : n(n), lg(std::log2(n)), graph(n), depth(n, 0), weighted_depth(n, 0), prt(lg + 1, std::vector(n)) {} // Insert an edge. void insert (size_t u, size_t v, T cost = 1) { graph.at(u).emplace_back(v, cost); graph.at(v).emplace_back(u, cost); } // Build a doubling table. void build (size_t root = 0) { fix ([&](auto dfs, size_t crr, size_t p) -> void { prt.at(0).at(crr) = p; for (auto const& e : graph.at(crr)) { if (e.to == p) continue; depth.at(e.to) = depth.at(crr) + 1; weighted_depth.at(e.to) = weighted_depth.at(crr) + e.cost; dfs(e.to, crr); } })(root, root); for (size_t p = 1; p <= lg; p++) { for (size_t i = 0; i < n; i++) { prt.at(p).at(i) = prt.at(p - 1).at(prt.at(p - 1).at(i)); } } } // Calculate the lca. auto operator()(size_t u, size_t v) const -> size_t { if (depth.at(u) < depth.at(v)) std::swap(u, v); auto diff = depth.at(u) - depth.at(v); if (diff > 0) { for (size_t p = lg, q = std::pow(2, lg); p <= lg; p--, q /= 2) { if (diff > q) u = prt.at(p).at(u), diff -= q; } u = prt.at(0).at(u), diff--; } assert(diff == 0), assert(depth.at(u) == depth.at(v)); if (u == v) return u; for (size_t p = lg, q = std::pow(2, lg); p <= lg; p--, q /= 2) { auto next_u = prt.at(p).at(u); auto next_v = prt.at(p).at(v); if (next_u != next_v) u = next_u, v = next_v; } assert(u != v); u = prt.at(0).at(u), v = prt.at(0).at(v), assert(u == v); return u; } // Calculate the count-based distance. auto row_dist (size_t u, size_t v) const -> unsigned { return depth.at(u) + depth.at(v) - 2 * depth.at(operator()(u, v));} // Calculate the count-based distance. auto weighted_dist (size_t u, size_t v) const -> T { auto& x = weighted_depth; return x.at(u) + x.at(v) - 2 * x.at(operator()(u, v));} }; template auto make_vector_impl(size_t sz, T t) {return std::vector(sz, t);} template = nullptr> auto make_vector(size_t sz, U u) {return make_vector_impl(sz, T(u));} template = nullptr> auto make_vector(size_t sz) {return std::vector(sz);} template = nullptr> auto make_vector(size_t a, Args... args) {return make_vector_impl(a, make_vector(args...));} template auto& at(T& t, Size_t i) {return t.at(i);} template auto& at(T& t, Size_t i, Args... args) {return at(t.at(i), args...);} template < typename Container, typename Value = typename Container::value_type, std::enable_if_t::value, std::nullptr_t> = nullptr> std::istream& operator>> (std::istream& is, Container& v) { for (auto & x : v) { is >> x; } return is; } template < typename Container, typename Value = typename Container::value_type, std::enable_if_t::value, std::nullptr_t> = nullptr > std::ostream& operator<< (std::ostream& os, Container const& v) { os << "{"; for (auto it = v.begin(); it != v.end(); it++) {os << (it != v.begin() ? "," : "") << *it;} return os << "}"; } template < template < typename ... > class Tuple, typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value > std::istream& tuple_input_impl(std::istream& os, Tuple& tuple, std::integer_sequence) { (void)std::initializer_list{((void)(os >> std::get< Inds >(tuple)), 0)...}; return os; } template < template < typename ... > class Tuple, typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value > std::istream& operator>> (std::istream& os, Tuple& tuple) { return tuple_input_impl(os, tuple, std::index_sequence_for()); } template < template < typename ... > class Tuple, typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value > std::ostream& tuple_output_impl(std::ostream& os, const Tuple& tuple, std::integer_sequence) { os << "("; (void)std::initializer_list{((void)(os << (Inds > 0 ? "," : "") << std::get< Inds >(tuple)), 0)...}; return os << ")"; } template < template < typename ... > class Tuple, typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value > std::ostream& operator<< (std::ostream& os, const Tuple& tuple) { return tuple_output_impl(os, tuple, std::index_sequence_for()); } int main() { std::cin.tie(0); std::cin.sync_with_stdio(false); int n; std::cin >> n; auto lca = lowest_common_ancestor< lint >(n); loop(n - 1) { int u, v, w; std::cin >> u >> v >> w; lca.insert(u, v, w); } lca.build(); int q; std::cin >> q; loop(q) { int x, y, z; std::cin >> x >> y >> z; auto ans = lca.weighted_dist(x, y) + lca.weighted_dist(y, z) + lca.weighted_dist(z, x); ans /= 2; std::cout << ans << std::endl; } return 0; }