#include using namespace std; const int mod = 1e9 + 7; using int64 = long long; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 > &p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in : v) is >> in; return is; } template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template< typename T = int64 > vector< T > make_v(size_t a) { return vector< T >(a); } template< typename T, typename... Ts > auto make_v(size_t a, Ts... ts) { return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...)); } template< typename T, typename V > typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) { t = v; } template< typename T, typename V > typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) { for(auto &e : t) fill_v(e, v); } template< typename F > struct FixPoint : F { FixPoint(F &&f) : F(forward< F >(f)) {} template< typename... Args > decltype(auto) operator()(Args &&... args) const { return F::operator()(*this, forward< Args >(args)...); } }; template< typename F > inline decltype(auto) MFP(F &&f) { return FixPoint< F >{forward< F >(f)}; } template< typename Monoid = int > struct SplayTree { using F = function< Monoid(Monoid, Monoid) >; using S = function< Monoid(Monoid) >; struct Node { Node *l, *r, *p; int idx; Monoid key, sum; int sz; bool rev; bool is_root() { return !p; } Node(int idx, const Monoid &key) : idx(idx), key(key), sum(key), sz(1), rev(false), l(nullptr), r(nullptr), p(nullptr) {} }; const F f; const S s; SplayTree(const F &f, const S &s) : f(f), s(s) {} Node *make_node(int idx, const Monoid &v = Monoid()) { return new Node(idx, v); } void toggle(Node *t) { swap(t->l, t->r); t->sum = s(t->sum); t->rev ^= true; } void push(Node *t) { if(t->rev) { if(t->l) toggle(t->l); if(t->r) toggle(t->r); t->rev = false; } } void update(Node *t) { t->sz = 1; t->sum = t->key; if(t->l) t->sz += t->l->sz, t->sum = f(t->l->sum, t->sum); if(t->r) t->sz += t->r->sz, t->sum = f(t->sum, t->r->sum); } void rotr(Node *t) { auto *x = t->p, *y = x->p; if((x->l = t->r)) t->r->p = x; t->r = x, x->p = t; update(x), update(t); if((t->p = y)) { if(y->l == x) y->l = t; if(y->r == x) y->r = t; update(y); } } void rotl(Node *t) { auto *x = t->p, *y = x->p; if((x->r = t->l)) t->l->p = x; t->l = x, x->p = t; update(x), update(t); if((t->p = y)) { if(y->l == x) y->l = t; if(y->r == x) y->r = t; update(y); } } void splay(Node *t) { push(t); while(!t->is_root()) { auto *q = t->p; if(q->is_root()) { if(q->l == t) rotr(t); else rotl(t); } else { auto *r = q->p; if(r->l == q) { if(q->l == t) rotr(q), rotr(t); else rotl(t), rotr(t); } else { if(q->r == t) rotl(q), rotl(t); else rotr(t), rotl(t); } } } } void link(Node *child, Node *parent) { splay(child); splay(parent); child->p = parent; parent->r = child; update(parent); } void cut(Node *child) { splay(child); auto *parent = child->l; child->l = nullptr; parent->p = nullptr; update(child); } void evert(Node *t) { splay(t); toggle(t); push(t); } Node *find_first(Node *t) { splay(t); while(t->l) { t = t->l; push(t); } return t; } Node *find_last(Node *t) { splay(t); while(t->r) { t = t->r; push(t); } return t; } Node *find_next(Node *t) { splay(t); if(!t->r) return nullptr; t = t->r; push(t); while(t->l) { t = t->l; push(t); } return t; } Node *find_prev(Node *t) { splay(t); if(!t->l) return nullptr; t = t->l; push(t); while(t->r) { t = t->r; push(t); } return t; } }; template< typename Monoid > struct EulerTourTree : SplayTree< Monoid > { using ST = SplayTree< Monoid >; using ST::SplayTree; using Node = typename ST::Node; using Ver = pair< Node *, Node * >; int left_size(Node *t) { ST::splay(t); return t->l ? t->l->sz : 0; } int ord(Ver &p) { modify(p); return left_size(p.first); } void modify(Ver &p) { int fsz = left_size(p.first); int ssz = left_size(p.second); if(fsz > ssz) swap(p.first, p.second); } void cut(Ver p) { modify(p); Node *pre = ST::find_prev(p.first); ST::cut(p.first); Node *nxt = ST::find_next(p.second); ST::cut(nxt); ST::link(nxt, pre); } void link(Ver child, Ver parent) { modify(child); modify(parent); ST::cut(parent.second); Node *nxt = ST::find_last(parent.first); ST::link(child.first, nxt); ST::link(parent.second, child.second); } void reroot(Ver p) { modify(p); ST::cut(p.second); ST::evert(p.first); Node *nxt = ST::find_last(p.first); ST::link(p.second, nxt); } Ver make_ver(const int &idx, const Monoid &k) { auto s = ST::make_node(idx, k); auto t = ST::make_node(idx, k); ST::link(t, s); return {s, t}; } }; template< typename G > struct DoublingLowestCommonAncestor { const int LOG; vector< int > dep; const G &g; vector< vector< int > > table; vector< int64 > dist; DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())), dist(g.size()) { table.assign(LOG, vector< int >(g.size(), -1)); } void dfs(int idx, int par, int d) { table[0][idx] = par; dep[idx] = d; for(auto &to : g[idx]) { if(to != par) { dist[to] = dist[idx] + to.cost; dfs(to, idx, d + 1); } } } void build() { dfs(0, -1, 0); for(int k = 0; k + 1 < LOG; k++) { for(int i = 0; i < table[k].size(); i++) { if(table[k][i] == -1) table[k + 1][i] = -1; else table[k + 1][i] = table[k][table[k][i]]; } } } int query(int u, int v) { if(dep[u] > dep[v]) swap(u, v); for(int i = LOG - 1; i >= 0; i--) { if(((dep[v] - dep[u]) >> i) & 1) v = table[i][v]; } if(u == v) return u; for(int i = LOG - 1; i >= 0; i--) { if(table[i][u] != table[i][v]) { u = table[i][u]; v = table[i][v]; } } return table[0][u]; } int64 query_dist(int u, int v) { return dist[u] + dist[v] - 2 * dist[query(u, v)]; } }; template< typename T > struct edge { int src, to; T cost; edge(int to, T cost) : src(-1), to(to), cost(cost) {} edge(int src, int to, T cost) : src(src), to(to), cost(cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template< typename T > using Edges = vector< edge< T > >; template< typename T > using WeightedGraph = vector< Edges< T > >; using UnWeightedGraph = vector< vector< int > >; template< typename T > using Matrix = vector< vector< T > >; int main() { int N, Q; cin >> N; using ETT = EulerTourTree< int >; auto f = [](int a, int b) { return a; }; auto s = [](int a) { return a; }; ETT ett(f, s); vector< ETT::Ver > ver(N); for(int i = 0; i < N; i++) ver[i] = ett.make_ver(i, 0); WeightedGraph< int64 > g(N); for(int i = 1; i < N; i++) { int a, b, c; cin >> a >> b >> c; ett.reroot(ver[a]); ett.link(ver[a], ver[b]); g[a].emplace_back(b, c); g[b].emplace_back(a, c); } DoublingLowestCommonAncestor< WeightedGraph< int64 > > qs(g); qs.build(); cin >> Q; for(int i = 0; i < Q; i++) { int K; cin >> K; vector< pair< int, int > > T(K); for(auto &p : T) { cin >> p.second; p.first = ett.ord(ver[p.second]); } sort(begin(T), end(T)); int64 ret = 0; for(int j = 0; j < T.size(); j++) { ret += qs.query_dist(T[j].second, T[(j + 1) % T.size()].second); } cout << ret / 2 << "\n"; } }