# Luogu P2495 [SDOI2011]消耗战

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define R register int
#define ll long long
using namespace std;
namespace Luitaryi {
inline int g() { R x=0,f=1;
register char s; while(!isdigit(s=getchar())) f=s=='-'?-1:f;
do x=x*10+(s^48); while(isdigit(s=getchar())); return x*f;
} const int N=250010; const ll Inf=1e15;
int n,m,k;
int d[N],dfn[N],h[N],stk[N],pre[N],son[N],sz[N],top[N],t,num;
ll ans[N],mn[N];
bool vis[N];
int vr[N<<1],nxt[N<<1],fir[N],w[N<<1],cnt;
inline void add(int u,int v,int ww)
{vr[++cnt]=v,nxt[cnt]=fir[u],fir[u]=cnt,w[cnt]=ww;}
inline void dfs(int u) {
dfn[u]=++num,sz[u]=1;
for(R i=fir[u];i;i=nxt[i]) {
R v=vr[i];
if(d[v]) continue;
mn[v]=min(mn[u],(ll)w[i]),d[v]=d[u]+1,pre[v]=u;
dfs(v); sz[u]+=sz[v];
if(sz[son[u]]<sz[v]) son[u]=v;
}
}
inline void dfs2(int u,int tp) {
top[u]=tp;
if(son[u]) dfs2(son[u],tp);
for(R i=fir[u];i;i=nxt[i]) {
R v=vr[i];
if(!top[v]) dfs2(v,v);
}
}
inline int lca(int u,int v) {
while(top[u]!=top[v]) {
if(d[top[u]]<d[top[v]]) swap(u,v);
u=pre[top[u]];
} return d[u]<d[v]?u:v;
}
inline void dp(int u) {
ans[u]=0;
for(R i=fir[u];i;i=nxt[i]) {
R v=vr[i];
dp(v);
ans[u]+=ans[v];
}
if(vis[u]) ans[u]=mn[u];
else ans[u]=min(ans[u],(ll)mn[u]);
fir[u]=0;
}
inline bool cmp(const int& a,const int& b)
{return dfn[a]<dfn[b];}
inline void main() {
n=g();
for(R i=1,u,v,w;i<n;++i)
mn[1]=Inf,d[1]=1,dfs(1),num=0,dfs2(1,0);
memset(fir,0,n+1<<2);
m=g(); while(m--) {
k=g(); for(R i=1;i<=k;++i)
h[i]=g(),vis[h[i]]=true;
sort(h+1,h+k+1,cmp);
cnt=0,fir[1]=0;
R top;
stk[top=1]=1;
for(R i=1+(h[1]==1),l;i<=k;++i) {
l=lca(stk[top],h[i]);
if(l!=stk[top]) {
while(dfn[l]<dfn[stk[top-1]]) {
lca(stk[top-1],stk[top]);
--top;
}
if(dfn[l]>dfn[stk[top-1]]) {
lca(l,stk[top]);
}
}
stk[++top]=h[i];
}
for(R i=1;i<top;++i)