#include template class Edge{ public: int from,to; Cost cost; Edge() {} Edge(int to, Cost cost): to(to), cost(cost){} Edge(int from, int to, Cost cost): from(from), to(to), cost(cost){} }; template using Graph = std::vector>>; template using Tree = std::vector>>; template void add_edge(C &g, int from, int to, T w = 1){ g[from].emplace_back(from, to, w); } template void add_undirected(C &g, int a, int b, T w = 1){ add_edge(g, a, b, w); add_edge(g, b, a, w); } template class HLDecomposition{ Tree tree; int n; std::vector sub, // subtree size par, // parent id head, // chain head id id, // id[original id] = hld id rid, // rid[hld id] = original id next, // next node in a chain end; // int dfs_sub(int cur, int p){ par[cur] = p; int t = 0; for(auto &e : tree[cur]){ if(e.to == p) continue; sub[cur] += dfs_sub(e.to, cur); if(sub[e.to] > t){ t = sub[e.to]; next[cur] = e.to; std::swap(e, tree[cur][0]); } } return sub[cur]; } void dfs_build(int cur, int &i){ id[cur] = i; rid[i] = cur; ++i; for(auto &e : tree[cur]){ if(e.to == par[cur]) continue; head[e.to] = (e.to == tree[cur][0].to ? head[cur] : e.to); dfs_build(e.to, i); } end[cur] = i; } public: HLDecomposition(const Tree &tree, int root): tree(tree), n(tree.size()), sub(n,1), par(n,-1), head(n), id(n), rid(n), next(n,-1), end(n,-1){ dfs_sub(root, -1); int i = 0; dfs_build(root, i); } template // std::function void path_query_vertex(int x, int y, const Func &f) const { while(1){ if(id[x] > id[y]) std::swap(x, y); f(std::max(id[head[y]], id[x]), id[y]+1); if(head[x] == head[y]) return; y = par[head[y]]; } } template void path_query_vertex(int x, int y, const LeftFunc &f, const RightFunc &g) const { const int w = lca(x, y); path_query_vertex(x, w, f); x = y; y = w; while(1){ if(id[x] > id[y]){ std::swap(x, y); } g(std::max({id[head[y]], id[x], id[w]+1}), id[y]+1); if(head[x] == head[y]) return; y = par[head[y]]; } } template // std::function void path_query_edge(int x, int y, const Func &f) const { while(1){ if(id[x] > id[y]) std::swap(x, y); if(head[x] == head[y]){ if(x != y) f(id[x]+1, id[y]+1); return; } f(id[head[y]], id[y]+1); y = par[head[y]]; } } template // std::function void subtree_query_edge(int x, const Func &f) const { f(id[x]+1, end[x]); } template // std::function void subtree_query_vertex(int x, const Func &f) const { f(id[x], end[x]); } int get_edge_id(int u, int v) const { // 辺に対応するid if(par[u] == v){ return id[u]; }else if(par[v] == u){ return id[v]; } return -1; } int parent(int x) const {return par[x];}; int lca(int u, int v) const { while(1){ if(id[u] > id[v]) std::swap(u, v); if(head[u] == head[v]) return u; v = par[head[v]]; } } int get_id(int x) const { return id[x]; } }; template class SegmentTree{ using value_type = typename Monoid::value_type; int depth, size, hsize; std::vector data; public: SegmentTree(){} SegmentTree(int n): depth(n > 1 ? 32-__builtin_clz(n-1) + 1 : 1), size(1 << depth), hsize(size / 2), data(size, Monoid::id()) {} inline auto operator[](int i) const {return at(i);} inline auto at(int i) const {return data[hsize + i];} inline auto get(int x, int y) const { // [x,y) value_type ret_left = Monoid::id(); value_type ret_right = Monoid::id(); int l = x + hsize, r = y + hsize; while(l < r){ if(r & 1) ret_right = Monoid::op(data[--r], ret_right); if(l & 1) ret_left = Monoid::op(ret_left, data[l++]); l >>= 1, r >>= 1; } return Monoid::op(ret_left, ret_right); } inline void update(int i, const value_type &x){ i += hsize; data[i] = x; while(i > 1) i >>= 1, data[i] = Monoid::op(data[i << 1 | 0], data[i << 1 | 1]); } template inline void init_with_vector(const std::vector &val){ data.assign(size, Monoid::id()); for(int i = 0; i < (int)val.size(); ++i) data[hsize + i] = val[i]; for(int i = hsize-1; i >= 1; --i) data[i] = Monoid::op(data[i << 1 | 0], data[i << 1 | 1]); } template inline void init(const T &val){ init_with_vector(std::vector(hsize, val)); } }; template struct SumMonoid{ using value_type = T; constexpr inline static value_type id(){return 0;} constexpr inline static value_type op(const value_type &a, const value_type &b){return a + b;} }; template struct AuxiliaryTree{ std::vector preorder; void dfs(const Tree &tree, int cur, int par, int &&i){ preorder[cur] = i; ++i; for(auto &e : tree[cur]){ if(e.to == par) continue; dfs(tree, e.to, cur, std::forward(i)); } } AuxiliaryTree(const Tree &tree, int root){ const int N = tree.size(); preorder.assign(N, 0); dfs(tree, root, -1, 0); } template auto build(std::vector vs, LCAFunc f){ std::sort(vs.begin(), vs.end(), [&](int i, int j){return preorder[i] < preorder[j];}); int n = vs.size(); for(int i = 0; i < n-1; ++i){ int x = f(vs[i], vs[i+1]); vs.push_back(x); } std::sort(vs.begin(), vs.end(), [&](int i, int j){return preorder[i] < preorder[j];}); vs.erase(std::unique(vs.begin(), vs.end()), vs.end()); return vs; } }; int main(){ int N; std::cin >> N; Tree tree(N); for(int i = 0; i < N-1; ++i){ int u, v; std::cin >> u >> v; int64_t w; std::cin >> w; add_undirected(tree, u, v, w); } HLDecomposition hld(tree, 0); AuxiliaryTree aux(tree, 0); SegmentTree> seg(N); for(int i = 0; i < N; ++i){ for(auto &e : tree[i]){ if(e.from < e.to){ seg.update(hld.get_edge_id(e.from, e.to), e.cost); } } } int Q; std::cin >> Q; while(Q--){ int k; std::cin >> k; std::vector x(k); for(int i = 0; i < k; ++i) std::cin >> x[i]; x = aux.build(x, [&](int i, int j){return hld.lca(i, j);}); k = x.size(); int64_t ans = 0; for(int i = 0; i < k; ++i){ hld.path_query_edge( x[i], x[(i+1)%k], [&](int i, int j){ ans += seg.get(i, j); } ); } ans /= 2; std::cout << ans << "\n"; } return 0; }