#line 1 "library/Template/template.hpp" #include using namespace std; #define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define ALL(v) (v).begin(),(v).end() using ll=long long int; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; templateinline bool chmax(T& a,T b){if(ainline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} #line 2 "library/Utility/fastio.hpp" #include class FastIO{ static constexpr int L=1<<16; char rdbuf[L]; int rdLeft=0,rdRight=0; inline void reload(){ int len=rdRight-rdLeft; memmove(rdbuf,rdbuf+rdLeft,len); rdLeft=0,rdRight=len; rdRight+=fread(rdbuf+len,1,L-len,stdin); } inline bool skip(){ for(;;){ while(rdLeft!=rdRight and rdbuf[rdLeft]<=' ')rdLeft++; if(rdLeft==rdRight){ reload(); if(rdLeft==rdRight)return false; } else break; } return true; } template::value,int> =0>inline bool _read(T& x){ if(!skip())return false; if(rdLeft+20>=rdRight)reload(); bool neg=false; if(rdbuf[rdLeft]=='-'){ neg=true; rdLeft++; } x=0; while(rdbuf[rdLeft]>='0' and rdLeft::value,int> =0>inline bool _read(T& x){ if(!skip())return false; if(rdLeft+20>=rdRight)reload(); bool neg=false; if(rdbuf[rdLeft]=='-'){ neg=true; rdLeft++; } x=0; while(rdbuf[rdLeft]>='0' and rdbuf[rdLeft]<='9' and rdLeft='0' and rdbuf[rdLeft]<='9' and rdLeft=rdRight)reload(); x=rdbuf[rdLeft++]; return true; } inline bool _read(string& x){ if(!skip())return false; for(;;){ int pos=rdLeft; while(pos' ')pos++; x.append(rdbuf+rdLeft,pos-rdLeft); if(rdLeft==pos)break; rdLeft=pos; if(rdLeft==rdRight)reload(); else break; } return true; } templateinline bool _read(vector& v){ for(auto& x:v){ if(!_read(x))return false; } return true; } char wtbuf[L],tmp[50]; int wtRight=0; inline void flush(){ fwrite(wtbuf,1,wtRight,stdout); wtRight=0; } inline void _write(const char& x){ if(wtRight>L-32)flush(); wtbuf[wtRight++]=x; } inline void _write(const string& x){ for(auto& c:x)_write(c); } template::value,int> =0>inline void _write(T x){ if(wtRight>L-32)flush(); if(x==0){ _write('0'); return; } else if(x<0){ _write('-'); if (__builtin_expect(x == std::numeric_limits::min(), 0)) { switch (sizeof(x)) { case 2: _write("32768"); return; case 4: _write("2147483648"); return; case 8: _write("9223372036854775808"); return; } } x=-x; } int pos=0; while(x!=0){ tmp[pos++]=char((x%10)|48); x/=10; } rep(i,0,pos)wtbuf[wtRight+i]=tmp[pos-1-i]; wtRight+=pos; } templateinline void _write(const vector& v){ rep(i,0,v.size()){ if(i)_write(' '); _write(v[i]); } } public: FastIO(){} ~FastIO(){flush();} inline void read(){} template inline void read(Head& head,Tail&... tail){ assert(_read(head)); read(tail...); } templateinline void write(){if(ln)_write('\n');} template inline void write(const Head& head,const Tail&... tail){ if(space)_write(' '); _write(head); write(tail...); } }; /** * @brief Fast IO */ #line 3 "sol.cpp" #line 2 "library/Graph/lca.hpp" struct LCA{ LCA(int _n=0):n(_n),g(_n),depth(_n+1,inf),start(_n){} void add_edge(int u,int v){ g[u].push_back(v); g[v].push_back(u); } void run(int root=0){ depth[root]=0; dfs(root,-1); N=1; while(N0;i--)tree[i]=op(tree[i*2],tree[i*2+1]); } int lca(int u,int v){ int a=start[u],b=start[v]; if(a>b)swap(a,b); b++; int res=n; for(int T=b-a;T>=1;T=b-a){ int x=a|((1U<<31)>>__builtin_clz(T)); int y=x&-x,k=__builtin_ctz(x); res=op(res,tree[(N|a)>>k]); a+=y; } return res; } private: int n,N; vector> g; vector depth,start,euler,tree; void dfs(int v,int p){ start[v]=euler.size(); euler.push_back(v); for(auto& to:g[v])if(to!=p){ depth[to]=depth[v]+1; dfs(to,v); euler.push_back(v); } } int op(int u,int v){ if(depth[u] in,dep; vector> _g,g; AuxiliaryTree(int _n):n(_n),pos(0),lca(n),in(n),dep(n),_g(n),g(n){} void add_edge(int u,int v){ lca.add_edge(u,v); _g[u].push_back(v); _g[v].push_back(u); } void run(int root=0){ lca.run(root); dfs(root,-1); } void query(vector& vs){ sort(ALL(vs),[&](int u,int v){return in[u] st; st.push(vs[0]); rep(i,0,m-1){ int w=lca.lca(vs[i],vs[i+1]); if(w!=vs[i]){ int cur=st.top(); st.pop(); while(!st.empty() and dep[w]1){ int c=st.top(); st.pop(); add(st.top(),c); } } void clear(vector& vs){ for(auto& w:vs)g[w].clear(); } private: void dfs(int v,int p){ in[v]=pos++; for(auto& to:_g[v])if(to!=p){ dep[to]=dep[v]+1; dfs(to,v); } } void add(int u,int v){ g[u].push_back(v); g[v].push_back(u); } }; /** * @brief Auxiliary Tree(Virtual Tree) */ #line 6 "sol.cpp" FastIO io; int main(){ int n; io.read(n); AuxiliaryTree g(n); LCA lca(n); using P=pair; vector tree(n,vector

()); rep(_,0,n-1){ int u,v,w; io.read(u,v,w); g.add_edge(u,v); lca.add_edge(u,v); tree[u].push_back({v,w}); tree[v].push_back({u,w}); } g.run(); lca.run(); vector dep(n); auto dfs=[&](auto& dfs,int v,int p)->void{ for(auto& [to,w]:tree[v])if(to!=p){ dep[to]=dep[v]+w; dfs(dfs,to,v); } }; dfs(dfs,0,-1); auto dist=[&](int u,int v)->ll{ return dep[u]+dep[v]-2*dep[lca.lca(u,v)]; }; int q; io.read(q); while(q--){ int k; io.read(k); vector vs(k); io.read(vs); g.query(vs); ll ret=0; for(auto& v:vs)for(auto& to:g.g[v])ret+=dist(v,to); ret/=2; io.write(ret); g.clear(vs); } return 0; }