#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 111111
inline int read()
{
    RG int x=0,t=1;RG char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-')t=-1,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
    return x*t;
}
struct Line{int v,next;}e[MAX<<1];
int h[MAX],cnt=1;
inline void Add(int u,int v){e[cnt]=(Line){v,h[u]};h[u]=cnt++;}
int fa[MAX],dep[MAX],size[MAX],hson[MAX],dfn[MAX],low[MAX],top[MAX],tim;
void dfs1(int u,int ff)
{
    fa[u]=ff;dep[u]=dep[ff]+1;size[u]=1;
    for(int i=h[u];i;i=e[i].next)
    {
        int v=e[i].v;if(v==ff)continue;
        dfs1(v,u);size[u]+=size[v];
        if(size[v]>size[hson[u]])hson[u]=v;
    }
}
void dfs2(int u,int tp)
{
    top[u]=tp;dfn[u]=++tim;
    if(hson[u])dfs2(hson[u],tp);
    for(int i=h[u];i;i=e[i].next)
        if(e[i].v!=fa[u]&&e[i].v!=hson[u])
            dfs2(e[i].v,e[i].v);
    low[u]=tim;
}
int LCA(int u,int v)
{
    while(top[u]^top[v])dep[top[u]]<dep[top[v]]?v=fa[top[v]]:u=fa[top[u]];
    return dep[u]<dep[v]?u:v;
}
int p[MAX<<1],S[MAX];
bool cmp(int a,int b){return dfn[a]<dfn[b];}
int f[MAX],g[MAX],n,Q,K;
bool FL=false,vis[MAX];
void DP(int u)
{
    for(int i=h[u];i;i=e[i].next)
    {
        int v=e[i].v;DP(v);
        f[u]+=f[v];g[u]+=g[v];
    }
    if(vis[u])f[u]+=g[u],g[u]=1;
    else f[u]+=(g[u]>1),g[u]=(g[u]==1);
}
int Calc(int rt)
{
    DP(rt);
    for(int i=1;i<=K;++i)if(vis[p[i]]&&vis[fa[p[i]]])return -1;
    return f[rt];
}
int main()
{
    n=read();
    for(int i=1,u,v;i<n;++i)u=read(),v=read(),Add(u,v),Add(v,u);
    dfs1(1,0);dfs2(1,1);
    memset(h,0,sizeof(h));
    Q=read();
    while(Q--)
    {
        K=read();cnt=1;
        for(int i=1;i<=K;++i)vis[p[i]=read()]=true;
        sort(&p[1],&p[K+1],cmp);
        for(int i=K;i>1;--i)p[++K]=LCA(p[i],p[i-1]);
        sort(&p[1],&p[K+1],cmp);K=unique(&p[1],&p[K+1])-p-1;
        for(int i=1,tp=0;i<=K;++i)
        {
            while(tp&&low[S[tp]]<dfn[p[i]])--tp;
            Add(S[tp],p[i]);S[++tp]=p[i];
        }
        printf("%d\n",Calc(p[1]));
        for(int i=1;i<=K;++i)h[p[i]]=0,vis[p[i]]=false,f[p[i]]=g[p[i]]=0;
    }
    return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define Int register int
#define MAXN 100005

vector <int> G[MAXN],G1[MAXN];

int n,m,cnt = 1,Index;
int h[MAXN],f[MAXN],g[MAXN];
int s[MAXN],son[MAXN],low[MAXN],ask[MAXN],top[MAXN],dfn[MAXN],par[MAXN],size[MAXN],deep[MAXN];

bool vis[MAXN];

struct line
{
	int v,next;
}e[MAXN << 1];

void Add (int u,int v)
{
	e[cnt] = line{v,h[u]};
	h[u] = cnt ++;
}

void read (int &x)
{
	x = 0;char c = getchar();int f = 1;
	while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}
	while (c >= '0' && c <= '9'){x = (x << 3) + (x << 1) + c - '0';c = getchar();}
	x *= f;return ;
}

void write (int x)
{
	if (x < 0){x = -x;putchar ('-');}
	if (x > 9) write (x / 10);
	putchar (x % 10 + '0');
}

void dfs (int u,int fa)
{
	par[u] = fa;
	size[u] = 1;
	for (Int i = h[u];i;i = e[i].next)
	{
		int v = e[i].v;
		if (v == fa) continue;
		deep[v] = deep[u] + 1;
		dfs (v,u);
		size[u] += size[v];
		if (size[v] > size[son[u]]) son[u] = v;
	}
}

void dfs1 (int u,int Top)
{
	top[u] = Top;
	dfn[u] = ++ Index;
	if (son[u]) dfs1 (son[u],Top);
	for (Int i = h[u];i;i = e[i].next)
	{
		int v = e[i].v;
		if (v == par[u] || v == son[u]) continue;
		dfs1 (v,v);
	}
	low[u] = Index;
	return ;
}

int LCA (int u,int v)
{
	while (top[u] != top[v])
	{
		if (deep[top[u]] < deep[top[v]]) v = par[top[v]];
		else u = par[top[u]];
	}
	return deep[u] < deep[v] ? u : v;
}

bool cmp (int a,int b)
{
	return dfn[a] < dfn[b];
}

void DP (int u)
{
	for (Int i = h[u];i;i = e[i].next)
	{
		int v = e[i].v;
		DP (v);
		f[u] += f[v];
		g[u] += g[v];
	}
	if (vis[u]) f[u] += g[u],g[u] = 1;
	else f[u] += (g[u] > 1),g[u] = (g[u] == 1);
}

signed main()
{
	read (n);
	for (int i = 1;i < n;++ i)
	{
		int u,v;
		read (u),read (v);
		Add (u,v);
		Add (v,u);
	}
	dfs (1,0);
	dfs1 (1,1);
	memset (h,0,sizeof (h));
	read (m);
	for (Int i = 1;i <= m;++ i)
	{
		int k;
		read (k);
		for (Int j = 1;j <= k;++ j)
			read (ask[j]),vis[ask[j]] = 1;
		sort (ask + 1,ask + k + 1,cmp);
		int last_k = k;
		for (Int j = last_k;j > 1;-- j)
			ask[++ k] = LCA (ask[j],ask[j - 1]);
		sort (ask + 1,ask + k + 1,cmp);
		k = unique (ask + 1,ask + k + 1) - ask - 1;
		for (Int j = 1,Top = 0;j <= k;++ j)
		{
			while (Top && low[s[Top]] < dfn[ask[j]]) Top --;
			Add (s[Top],ask[j]);
			s[++ Top] = ask[j];
		}
		bool flag = 1;
		for (Int j = 1;j <= k;++ j)
			if (vis[ask[j]] && vis[par[ask[j]]])
			{
				flag = 0;
				break;
			}
		if (!flag) puts ("-1");
		else
		{
			DP (ask[1]);
			write (f[ask[1]]),putchar ('\n');
		}
		memset (h,0,sizeof (h));
		memset (f,0,sizeof (f));
		memset (g,0,sizeof (g));
		memset (vis,0,sizeof (0));
	}
}


#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

#define rep(i,__l,__r) for(int i=__l,i##_end_=__r;i<=i##_end_;++i)
#define fep(i,__l,__r) for(int i=__l,i##_end_=__r;i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define pii pair<int,int>
#define Endl putchar('\n')
// #define FILEOI
// #define int long long

#ifdef FILEOI
# define MAXBUFFERSIZE 500000
    inline char fgetc(){
        static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
    }
# undef MAXBUFFERSIZE
# define cg (c=fgetc())
#else
# define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
inline int qread(){
    int x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>inline T swa(T& x,T& y){
    T tmp=x;
    x=y;
    y=tmp;
}
template<class T>void fwrit(const T x){
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=100000;
const int logMAXN=16;

int N,Q,K,a[(MAXN<<1)+5],ans;
bool vis[MAXN+5],flg,spe[MAXN+5];
int sum[MAXN+5];

class simpleTree{
public:
    struct edge{
        int to,nxt,w;
        edge(){}
        edge(const int T,const int N):to(T),nxt(N){w=1;}
        edge(const int T,const int N,const int W):to(T),nxt(N),w(W){}
    }e[(MAXN<<1)+5];
    int tail[MAXN+5],ecnt;
    int f[MAXN+5][logMAXN+5],dep[MAXN+5];
    inline void init(){
        rep(i,1,N)tail[i]=0;
        ecnt=0;
    }
    inline void add_edge(const int u,const int v){
        e[++ecnt]=edge(v,tail[u]);tail[u]=ecnt;
        e[++ecnt]=edge(u,tail[v]);tail[v]=ecnt;
    }
    virtual inline void buildtre(const int rt){
        dfs_tre(rt,0);
        rep(j,1,logMAXN)rep(i,1,N)
            f[i][j]=f[f[i][j-1]][j-1];
    }
    inline int findLca(int u,int v){
        if(dep[u]>dep[v])update(u,dep[v]);
        if(dep[v]>dep[u])update(v,dep[u]);
        if(u==v)return u;
        for(int i=logMAXN;i>=0;--i)if(f[u][i]!=f[v][i])
            u=f[u][i],v=f[v][i];
        return f[u][0];
    }
    inline int depDis(const int u,const int v,const bool version=0){
        if(!version)return fab(dep[u]-dep[v]);
        else{
            int lca=findLca(u,v);
            return dep[u]+dep[v]-2*dep[lca];
        }
    }
    simpleTree(){init();}
private:
    virtual void dfs_tre(const int u,const int pre){
        dep[u]=dep[pre]+1;
        for(int i=tail[u],v;i;i=e[i].nxt)if((v=e[i].to)!=pre){
            f[v][0]=u;
            dfs_tre(v,u);
        }
    }
    virtual inline void update(int& u,const int __lim){
        for(int i=logMAXN;i>=0;--i)if(dep[f[u][i]]>=__lim)
            u=f[u][i];
    }
};

class spTre:public simpleTree{
public:
    int in[MAXN+5],out[MAXN+5],ind;
    void dfs_tre(const int u,const int pre){
        dep[u]=dep[pre]+1;
        in[u]=++ind;
        for(int i=tail[u],v;i;i=e[i].nxt)if((v=e[i].to)!=pre){
            f[v][0]=u;
            dfs_tre(v,u);
        }
        out[u]=++ind;
    }
    spTre(){init();}
};

spTre tre;
simpleTree vtre;

inline void init(){
    N=qread();
    rep(i,1,N-1)tre.add_edge(qread(),qread());
    tre.buildtre(1);
}

inline bool cmp(const int a,const int b){
    int t1=(a>0)?tre.in[a]:tre.out[-a];
    int t2=(b>0)?tre.in[b]:tre.out[-b];
    return t1<t2;
}

inline void getArr(){
    qread(K);
    rep(i,1,K){
        a[i]=qread();
        vis[a[i]]=spe[a[i]]=true;
        sum[a[i]]=1;
    }
    sort(a+1,a+K+1,cmp);
    int tmp=K;
    rep(i,1,tmp-1){
        int lca=tre.findLca(a[i],a[i+1]);
        if(!vis[lca])a[++K]=lca,vis[lca]=true;
    }
    tmp=K;
    if(!vis[1])a[++K]=1,vis[1]=true;
    rep(i,1,tmp)a[++K]=-a[i];
    sort(a+1,a+K+1,cmp);

    // rep(i,1,K)writc(a[i],' ');
    // Endl;
}

int st[MAXN+5],top;
inline void getTre(){
    top=0;
    rep(i,1,K){
        if(a[i]>0)st[++top]=a[i];
        else{
            int v=st[top--];
            if(v==1)break;
            vtre.add_edge(st[top],v);
            // printf("add edge : %d to %d\n",st[top],v);
        }
    }
}

inline void treDp(const int u,const int pre){
    for(int i=vtre.tail[u],v;i;i=vtre.e[i].nxt)if((v=vtre.e[i].to)!=pre){
        // printf("treDp:>u == %d, v == %d\n",u,v);
        // printf("spe[u] == %d, spe[v] == %d, dis == %d\n",spe[u],spe[v],tre.depDis(u,v));
        if(spe[u] && spe[v] && tre.depDis(u,v)==1){
            // printf("because %d and %d\n",u,v);
            flg=true;
        }
        treDp(v,u);
        sum[u]+=sum[v];

        sum[v]=0;
        spe[v]=false;
    }
    if(sum[u]>=2)++ans,sum[u]=0;
    vtre.tail[u]=0;
}

inline void getQuery(){
    flg=false;
    getArr();
    getTre();
    treDp(1,0);
    if(!flg)writc(ans,'\n');
    else writc(-1,'\n');
    ans=0,sum[1]=0,vis[1]=spe[1]=false;
}

signed main(){
#ifdef FILEOI
    freopen("file.in","r",stdin);
    freopen("file.out","w",stdout);
#endif
    init();
    Q=qread();
    while(Q--)getQuery();
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

const int maxn=1e6+10;
const int maxlg=2e1;
const int inf=1e6+10;

struct Node
{
	int to,next;
}e[maxn<<1];
int head[maxn];
int fa[maxn][maxlg];
bool vis[maxn];
int deep[maxn];
int a[maxn];
int dfn[maxn];
int sta[maxn];
int dp[maxn];
int size[maxn];
bool go_home;
int t_cnt,cnt,top,num,rt,ans;
vector<int> vec[maxn];

void add(int x,int y)
{
	e[++cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}

void dfs(int x)
{
	dfn[x]=++t_cnt;
	for(int i=1;i<maxlg;i++)
		fa[x][i]=fa[fa[x][i-1]][i-1];
	for(int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if(v==fa[x][0]) continue;
		fa[v][0]=x,deep[v]=deep[x]+1;
		dfs(v);
	}
}

int cmp(const int &x,const int &y)
{
	return dfn[x]<dfn[y];
}

int query(int x,int y)
{
	if(deep[x]<deep[y]) swap(x,y);
	int now=deep[x]-deep[y];
	for(int i=0;now;i++) if(now&1<<i) now^=1<<i,x=fa[x][i];
	if(x==y) return x;
	for(int i=maxlg-1;i>=0;i--)
		if(fa[x][i]!=fa[y][i])
			x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}

void add_edge(int x,int y)
{
	vec[x].push_back(y);
	if(vis[x]&&vis[y]&&deep[x]+1==deep[y]) go_home=1;
}

void push_sta(int x)
{
	sta[++top]=x;
	if(deep[x]<deep[rt]) rt=x;
}

void insert(int x)
{
	if(!top)
	{
		push_sta(x);
		return;
	}
	int lca=query(x,sta[top]);
	if(sta[top]!=lca)
	{
		while(top>=2&&deep[sta[top-1]]>=deep[lca]) add_edge(sta[top],sta[top--]);
		if(lca!=sta[top]) add_edge(lca,sta[top--]),push_sta(lca);
	}
	push_sta(x);
}

void dfs2(int x)
{
	dp[x]=size[x]=0;
	for(int i=0;i<vec[x].size();i++)
	{
		int v=vec[x][i];
		dfs2(v);
		dp[x]+=dp[v],size[x]+=size[v];
	}
	if(vis[x]) dp[x]+=size[x],size[x]=1;
	else if(size[x]>1) dp[x]++,size[x]=0;
}

void dfs_clr(int x)
{
	for(int i=0;i<vec[x].size();i++)
	{
		int v=vec[x][i];
		dfs_clr(v);
	}
	vec[x].clear(),vis[x]=0,dp[x]=size[x]=0;
}

int main()
{
	int n,x,y,q,k;
	scanf("%d",&n);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		add(x,y),add(y,x);
	}
	dfs(1);deep[0]=inf;
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d",&k);
		for(int i=1;i<=k;i++) scanf("%d",&a[i]),vis[a[i]]=1;
		sort(a+1,a+k+1,cmp);
		go_home=0;top=rt=num=ans=0;
		for(int i=1;i<=k;i++)
		{
			insert(a[i]);
			if(go_home) break;
		}
		while(top>=2) add_edge(sta[top],sta[top--]);
		dfs2(rt);
		if(go_home) printf("-1\n");
		else printf("%d\n",dp[rt]);
		dfs_clr(rt);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct lsqy{
	int next,to;
}q[200001],e[200001];
int head[100001],cnt;
int Head[100001],ct;
int dfn[100001],dep[100001];
int n,m,d;
int ff[100001][22];
void add(int x,int y)
{
	q[++cnt].next=head[x];
	head[x]=cnt;
	q[cnt].to=y;
}
void dad(int x,int y)
{
	e[++ct].next=Head[x];
	Head[x]=ct;
	e[ct].to=y;
	e[++ct].next=Head[y];
	Head[y]=ct;
	e[ct].to=x;
}
int toot;
void dfs(int u,int fa)
{
	ff[u][0]=fa;
	dep[u]=dep[fa]+1;
	dfn[u]=++toot;
    for(int i=head[u];i;i=q[i].next)
    {
    	int v=q[i].to;
    	if(v==fa) continue;
    	dfs(v,u);
	}
}
void LCA()
{
    for(int i=1;i<=n;++i)
	  for(int j=1;j<=20;++j)
	    ff[i][j]=ff[ff[i][j-1]][j-1];
}
int lca(int x,int y){
	if(dep[x]<dep[y]) swap(x,y);
	int tmp=dep[x]-dep[y];
	for(int i=20;i>=0;i--)
	  if((1<<i)&tmp) x=ff[x][i];
	if(x==y) return x;
	for(int i=20;i>=0;i--)
	  if(ff[x][i]!=ff[y][i])
	    x=ff[x][i],y=ff[y][i];
	return ff[x][0];
}
bool cmp(int x,int y)
{
	return dfn[x]<dfn[y];
}

int can=0,di;
int sta[100001],t[100001],id[100001],dp[100001],g[100001];
//void dfs2(int u,int fa)
//{
//	for(int i=Head[u];i;i=e[i].next)
//	{
//		int v=e[i].to;
//		if(v==fa) continue;
//		dfs2(v,u);
//	}
//	dp[u]=g[u]=0;
//	if(t[u]==d) {
//		for(int i=Head[u];i;i=e[i].next)
//		{
//			int v=e[i].to;
//			if(t[v]==d&&abs(dep[u]-dep[v])==1) can=-1;
//			if(v==fa) continue;
//			dp[u]+=dp[v]+g[v];
//		 }g[u]=1;
//	}
//	else {
//		int sum=0;
//		for(int i=Head[u];i;i=e[i].next)
//		{
//			int v=e[i].to;
//			if(v==fa) continue;
//			dp[u]+=dp[v];
//			if(g[v]) sum++;
//		}
//		if(sum<=1) g[u]=sum;
//		else dp[u]++;
//	}
//	Head[u]=0;
// }
void dfs2(int now,int fa){
	//cout<<now<<' '<<fa<<' '<<is[now]<<' '<<id<<endl;
	for(int i=Head[now];i;i=e[i].next){
		int u=e[i].to;
		if(u==fa)continue;
		dfs2(u,now);
	}
	dp[now]=g[now]=0;
	if(t[now]==d){
		for(int i=Head[now];i;i=e[i].next){
			int u=e[i].to;
			if(t[u]==d && abs(dep[u]-dep[now])==1)can=-1;
			if(u==fa)continue;
			dp[now]+=dp[u]+g[u];
		}g[now]=1;
	}else{
		int Sum=0;
		for(int i=Head[now];i;i=e[i].next){
			int u=e[i].to;
			if(u==fa)continue;
			dp[now]+=dp[u];
			if(g[u])++Sum;
		}
		if(Sum<=1)g[now]=Sum;
		else dp[now]++;
	}
	Head[now]=0;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<n;++i)
	  {
	  	int u,v;
	  	scanf("%d%d",&u,&v);
	  	add(u,v);
	  	add(v,u);
	  }
	dfs(1,0);
	LCA();
	scanf("%d",&m);
	for(d=1;d<=m;++d)
	{
		int k,cnnt=can=0,top;
		scanf("%d",&k);
		for(int i=1;i<=k;++i)
		  {
            int x;
		  	scanf("%d",&x);
		  	t[x]=d;
		  	id[++cnnt]=x;
		  }
		sort(id+1,id+1+cnnt,cmp);
		sta[top=1]=1;
		for(int i=1;i<=cnnt;++i)
		  {
		  	if(id[i]==1) continue;
		  	int l=lca(sta[top],id[i]);
		  	while(dep[sta[top-1]]>dep[l])
		  	  {
		  	  	dad(sta[top-1],sta[top]);
		  	  	top--;
				}
			if(sta[top]!=l)
			{
				if(sta[top-1]==l) dad(sta[top-1],sta[top]),top--;
				else dad(l,sta[top]),sta[top]=l;
			}
			sta[++top]=id[i];
			}
		while(top>1)
		{
			dad(sta[top-1],sta[top]);
			top--;
		  }
		dfs2(1,0);
		if(!can) printf("%d\n",dp[1]);
	else puts("-1");
		ct=0;
	}

}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=1e5+5;
struct edge{
    int v,Next;
}e[N<<5];
int head[N],cnt,n,a[N],st[N],top;
void addedge(int u,int v){
    e[++cnt].v=v;
    e[cnt].Next=head[u];
    head[u]=cnt;
}
int fat[N][30],dfsn[N],Cnt,dep[N],sz[N],ans;
void dfs(int p){
    dfsn[p]=++Cnt; dep[p]=dep[fat[p][0]]+1;
    rep(i,1,25) fat[p][i]=fat[fat[p][i-1]][i-1];
    //printf("$ %d   %d\n",dep[p],dfsn[p]);
    for(int ne=head[p];ne;ne=e[ne].Next){
        if(e[ne].v==fat[p][0]) continue;
        fat[e[ne].v][0]=p;
        dfs(e[ne].v);
    }
}
int LCA(int u,int v){
    if(dep[u]<dep[v]) swap(u,v);
    per(i,25,0) if(dep[fat[u][i]]>=dep[v]) u=fat[u][i];
    if(u==v) return u;
    per(i,25,0) if(fat[u][i]!=fat[v][i]) u=fat[u][i],v=fat[v][i];
    return fat[u][0];
}
bool cmp(int a,int b){
    return dfsn[a]<dfsn[b];
}
void Dfs(int p,int fa){
    //printf("# %d\n",p);
    if(sz[p]){
        for(int ne=head[p];ne;ne=e[ne].Next){
            if(e[ne].v==fa) continue;
            Dfs(e[ne].v,p);
	    if(sz[e[ne].v]) sz[e[ne].v]=0,ans++;
        }
    }else{
        for(int ne=head[p];ne;ne=e[ne].Next){
            if(e[ne].v==fa) continue;
            Dfs(e[ne].v,p);
            sz[p]+=sz[e[ne].v];
            sz[e[ne].v]=0;
        }
        if(sz[p]>1) sz[p]=0,ans++;
    }
    head[p]=0;
}
int main(){
    scanf("%d",&n);
    rep(i,2,n){
        int u,v;
        scanf("%d%d",&u,&v);
        addedge(u,v); addedge(v,u);
    }
    dfs(1); memset(head,0,sizeof(head));
    int q,k;
    scanf("%d",&q);
    while(q--){
        bool flag=false;
        scanf("%d",&k);
        rep(i,1,k) scanf("%d",&a[i]),sz[a[i]]=1;
        sort(a+1,a+k+1,cmp);
        rep(i,1,k) if(sz[fat[a[i]][0]]){
            puts("-1"); rep(i,1,k) sz[a[i]]=0; flag=true; break;
        }
        if(flag) continue;
        st[top=1]=1;
        rep(i,a[1]==1?2:1,k){
            if(!top){
                st[top=1]=a[i];
                continue;
            }
            int lca=LCA(st[top],a[i]);
            while(top>1&&(dep[lca]<dep[st[top-1]])){
                addedge(st[top-1],st[top]);
                //printf("%d -> %d\n",st[top-1],st[top]);
                top--;
            }
            if(dep[lca]<dep[st[top]]) addedge(lca,st[top--]);
            if((!top)||(st[top]!=lca)) st[++top]=lca;
            st[++top]=a[i];
        }
        if(top) while(--top) addedge(st[top],st[top+1]);//,printf("%d -> %d\n",st[top],st[top+1]);
        ans=0;
        Dfs(1,0);
        printf("%d\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,u,v,q,k;
int tot,dfn[N],low[N],size[N],d[N],f[N],son[N],top[N];
bool vis[N];
int p[N<<1],sta[N],dp[N],g[N];
int cnt,head[N];
struct edge{int next,to;}e[N<<1];

inline void add(int u,int v)
{
	cnt++;
	e[cnt].next=head[u];
	e[cnt].to=v;
	head[u]=cnt;
}

void dfs(int u,int fa)
{
	size[u]=1;
	dfn[u]=++tot;
	for (register int i=head[u]; i; i=e[i].next)
	if (e[i].to!=fa)
	{
		d[e[i].to]=d[u]+1; f[e[i].to]=u;
		dfs(e[i].to,u);
		size[u]+=size[e[i].to];
		if (size[son[u]]<size[e[i].to]) son[u]=e[i].to;
	}
	low[u]=tot;
}

void dfs2(int u,int TP)
{
	top[u]=TP;
	if (son[u]) dfs2(son[u],TP);
	for (register int i=head[u]; i; i=e[i].next)
	if (e[i].to!=son[u] && e[i].to!=f[u]) dfs2(e[i].to,e[i].to);
}

inline int lca(int u,int v)
{
	while (top[u]!=top[v])
	{
		if (d[top[u]]<d[top[v]]) swap(u,v);
		u=f[top[u]];
	}
	if (d[u]>d[v]) swap(u,v);
	return u;
}

void DP(int u)
{
	for (register int i=head[u]; i; i=e[i].next)
	{
		DP(e[i].to);
		dp[u]+=dp[e[i].to]; g[u]+=g[e[i].to];
	}
	//dp[u]:u为根的子树的答案最小值
	//对于有些只有一个重要节点的子树，可以暂时保留这个节点 ，等到不得已必须处理时再处理，可以使得答案更优
	//所以再记录一个g[u]:u为根的子树，当答案最小时，还剩下没处理好的节点个数
	if (vis[u]) dp[u]+=g[u],g[u]=1;
	else dp[u]+=(g[u]>1),g[u]=(g[u]==1);
}

inline int solve(int u)
{
	for (register int i=1; i<=k; ++i) if (vis[p[i]] && vis[f[p[i]]]) return -1;
	DP(u);
	return dp[u];
}

inline bool cmp(int a,int b){return dfn[a]<dfn[b];}

int main(){
	scanf("%d",&n);
	for (register int i=1; i<n; ++i) scanf("%d%d",&u,&v),add(u,v),add(v,u);
	dfs(1,0); dfs2(1,1);
	scanf("%d",&q);
	memset(head,0,sizeof(head));
	while (q--)
	{
		scanf("%d",&k);
		for (register int i=1; i<=k; ++i) scanf("%d",&p[i]),vis[p[i]]=true;
		sort(p+1,p+k+1,cmp);
		for (register int i=k; i>1; --i) p[++k]=lca(p[i],p[i-1]);
		sort(p+1,p+k+1,cmp);
		k=unique(p+1,p+k+1)-p-1;
		cnt=0;
		for (register int i=1,top=0; i<=k; ++i)
		{
			while (top && low[sta[top]]<dfn[p[i]]) top--;
			add(sta[top],p[i]); sta[++top]=p[i];
		}
		printf("%d\n",solve(p[1]));
		for (register int i=1; i<=k; ++i) head[p[i]]=0,vis[p[i]]=false,dp[p[i]]=g[p[i]]=0;
	}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
inline void read(int &x)
{
	x=0;char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+c-48,c=getchar();
}
const int maxn=2e5+5,INF=0x3f3f3f3f;
int n,q,tot,dex,ct,first[maxn],dfn[maxn],dep[maxn],st[maxn];
vector<int> F;
struct Road{int u,t,nxt;}eage[maxn];
void add(int x,int y) {eage[++tot]=(Road){x,y,first[x]};first[x]=tot;}
struct ST_Table{
	int p,bin[20],len[maxn],f[maxn][20];
	void init()
	{
		p=log(ct)/log(2)+1;
		for(int i=bin[0]=1;i<=p;++i) bin[i]=bin[i-1]<<1;
		for(int i=1;i<=ct;++i) len[i]=log(i)/log(2);
		for(int j=1;j<=p;++j)
			for(int i=1;i+bin[j]-1<=ct;++i)
				if(dep[f[i][j-1]]<dep[f[i+bin[j-1]][j-1]]) f[i][j]=f[i][j-1];
				else f[i][j]=f[i+bin[j-1]][j-1];
	}
	int LCA(int x,int y)
	{
		if(st[x]>st[y]) swap(x,y);
		int l=len[st[y]-st[x]+1];
		if(dep[f[st[x]][l]]<dep[f[st[y]-bin[l]+1][l]]) return f[st[x]][l];
		else return f[st[y]-bin[l]+1][l];
	}
}ST;
int cmp(const int a,const int b) {return dfn[a]<dfn[b];}
void frdfs(int x,int fa)
{
	dep[x]=dep[fa]+1;dfn[x]=++dex;ST.f[st[x]=++ct][0]=x;
	for(int i=first[x];i;i=eage[i].nxt) if(eage[i].t!=fa) frdfs(eage[i].t,x),ST.f[++ct][0]=x;
}
struct Fake_Tree{
	int tp,st[maxn],is[maxn];
	long long dp[maxn][2];
	vector<int> bl;
	void ins(int x)
	{
		if(!tp) return st[++tp]=x,void();
		int lca=ST.LCA(st[tp],x);
		if(lca==st[tp]) return st[++tp]=x,void();
		while(tp>1&&dfn[lca]<=dfn[st[tp-1]]) add(st[tp-1],st[tp]),--tp;
		if(lca^st[tp]) add(lca,st[tp]),st[tp]=lca;
		st[++tp]=x;
	}
	void dfs(int x)
	{
		if(!first[x]) return dp[x][0]=INF,dp[x][1]=0,void();
		if(!is[x]) dp[x][1]=INF;
		long long S=0;
		for(int i=first[x],t;i;i=eage[i].nxt)
		{
			t=eage[i].t;
			dfs(t);
			if(is[x])
			{
				dp[x][0]=INF;
				dp[x][1]+=min(dp[t][0],dep[t]-dep[x]>1?dp[t][1]+1:INF);
			}
			else
			{
				S+=min(dp[t][1],dp[t][0]);
				dp[x][1]=min(min(dp[x][1]+dp[t][0],dep[t]-dep[x]>1?dp[x][1]+dp[t][1]+1:INF),dp[x][0]+dp[t][1]);
				dp[x][0]+=dp[t][0];
			}
		}
		if(!is[x]) dp[x][0]=min(dp[x][0],S+1);
		bl.push_back(x);
	}
	void init(vector<int> t)
	{
		sort(t.begin(),t.end(),cmp);
		ins(1);
		for(int i=0;i<t.size();++i) is[t[i]]=1;
		for(int i=0;i<t.size();++i) if(t[i]^1) ins(t[i]);
		while(tp>1) add(st[tp-1],st[tp]),tp--;tp=0;dfs(1);
		printf("%lld\n",min(dp[1][0],dp[1][1])<=n?min(dp[1][0],dp[1][1]):-1);
		for(int i=0;i<bl.size();++i) is[bl[i]]=first[bl[i]]=dp[bl[i]][0]=dp[bl[i]][1]=0;
		bl.clear();tot=0;
	}
}fkt;
int main()
{
	read(n);
	for(int i=1,x,y;i<n;++i) read(x),read(y),add(x,y),add(y,x);
	frdfs(1,0);ST.init();tot=0;
	for(int i=1;i<=n;++i) first[i]=0;
	read(q);
	for(int i=1,w;i<=q;++i)
	{
		read(w);F.resize(w);
		for(int j=0;j<w;++j) read(F[j]);
		fkt.init(F);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10,inf=1e9;
int tot,ind,a[N],dfn[N],dep[N],fa[N][21];
int s[N],tp,st[N],a2[N],dp[N][2];bool vis[N];
struct node{int to,next;}edge[N<<4];
void read(int &x){
	int f=1;x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	x*=f;
}
void add(int x,int y){
	edge[++tot]=(node){y,a[x]},a[x]=tot;
}
void add2(int x,int y){
	edge[++tot]=(node){y,a2[x]},a2[x]=tot;
}
void dfs(int x,int f){
	dfn[x]=++ind;
	fa[x][0]=f,dep[x]=dep[f]+1;
	for(int i=1;i<=20;i++)
		fa[x][i]=fa[fa[x][i-1]][i-1];
	for(int i=a[x];i;i=edge[i].next){
		int y=edge[i].to;
		if(y!=f)dfs(y,x);
	}
}
int lca(int x,int y){
	if(dep[x]<dep[y])swap(x,y);
	if(dep[x]>dep[y])
		for(int i=20;i>=0;i--)
			if(dep[fa[x][i]]>=dep[y])
				x=fa[x][i];
	if(x==y)return x;
	for(int i=20;i>=0;i--)
		if(fa[x][i]!=fa[y][i])
			x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}
bool cmp(int x,int y){
	return dfn[x]<dfn[y];
}
void dpfs(int x){
	int mx=-1;
	dp[x][0]=0;
	if(vis[x])dp[x][1]=inf;
	else dp[x][1]=1;
	for(int i=a2[x];i;i=edge[i].next){
		int y=edge[i].to;dpfs(y);
		if(vis[x])dp[x][0]+=min(dp[y][0]+1,dp[y][1]);
		else{
			mx=max(mx,dp[y][1]-dp[y][0]),dp[x][0]+=dp[y][1];
			dp[x][1]+=min(dp[y][0],dp[y][1]);
		}
	}
	if(!vis[x])dp[x][0]-=mx;
}
int main(){
	int n,i,x,y,q,k;
	read(n);
	for(i=1;i<n;i++)
		read(x),read(y),add(x,y),add(y,x);
	dfs(1,0),read(q);
	for(int Cas=1;Cas<=q;Cas++){
		read(k);
		int ok=1;
		for(i=1;i<=k;i++)read(s[i]);
		sort(s+1,s+1+k,cmp);
		for(i=1;i<=k;i++){
			if(vis[fa[s[i]][0]]){ok=0;break;}
			vis[s[i]]=1;
		}
		if(!ok)puts("-1");
		else{
			st[tp=1]=1;
			for(i=1;i<=k;i++){
				if(s[i]==1)continue;
				x=lca(s[i],st[tp]);
				if(x==st[tp])st[++tp]=s[i];
				else{
					while(tp>1&&dfn[x]<dfn[st[tp-1]])
						add2(st[tp-1],st[tp]),tp--;
					if(st[tp-1]!=x)add2(x,st[tp]),st[tp]=x;
					else add2(x,st[tp--]);
					st[++tp]=s[i];
				}
			}
			while(tp>1)add2(st[tp-1],st[tp]),tp--;
			dpfs(1),printf("%d\n",min(dp[1][0],dp[1][1]));
		}
		for(i=1;i<=k;i++)
			vis[s[i]]=a2[i]=0;
		a2[1]=tot=0;
	}
	return 0;
}


#include<bits/stdc++.h>
#define lp long long
#define maxx(a,b) ((a)>(b)?(a):(b))
#define minn(a,b) ((a)<(b)?(a):(b))
using namespace std;
const lp ml=5e5+101;
struct wg{lp to,ne;}v[ml];
lp n,q,in;
lp js,top,start,cnt,flag,tot;
lp head[ml];
lp t[21][ml],dep[ml],bh[ml];
lp p[ml],tk[ml],pd[ml],dfn[ml];
lp g[ml];
vector < lp > e[ml];
inline lp read(){
	lp r=0,f=1;
	char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c))r=(r<<1)+(r<<3)+(c^48),c=getchar();
	return r*f;
}
inline void write(lp x){
	if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);putchar(x%10+'0');
}
inline void port(lp x,char s){write(x);putchar(s);}
void add(lp a1,lp a2){v[++js].to=a2;v[js].ne=head[a1];head[a1]=js;}
void dfs(lp x,lp f){
	t[0][++top]=x;bh[x]=top;dfn[x]=++tot;
	for(lp i=head[x];i;i=v[i].ne){
		lp y=v[i].to;if(y==f)continue;
		dep[y]=dep[x]+1;
		dfs(y,x);t[0][++top]=x;
	}
}
void pre(){
	for(lp i=1;i<=20;i++){
	for(lp j=1;j<=top-(1ll<<i)+1;j++){
	t[i][j]=(dep[t[i-1][j]]<dep[t[i-1][j+(1ll<<(i-1))]]?
	 t[i-1][j]:t[i-1][j+(1ll<<(i-1))]);
		}
  }
}
lp st(lp l,lp r){
	if(bh[l]>bh[r])swap(l,r);
	lp lo=log2(bh[r]-bh[l]);
	return 	(dep[t[lo][bh[l]]]<dep[t[lo][bh[r]-(1ll<<lo)]]?
			 t[lo][bh[l]]:t[lo][bh[r]-(1ll<<lo)]);
}
lp cmp(lp x,lp y){return dfn[x]<dfn[y];}
void link(lp a,lp b){e[a].push_back(b);e[b].push_back(a);}
lp dp(lp x,lp fa){
	lp res=0,num=0;
	for(lp i=0;i<e[x].size();i++){
		lp y=e[x][i];if(y==fa)continue;
		res+=dp(y,x);num+=g[y];
	}
	if(pd[x])res+=num,g[x]=1;
	else if(num>1)res++,g[x]=0;
	else g[x]=num?1:0;
	e[x].clear(),pd[x]=0;
	return res;
}
void insert(){top=0;tk[++top]=1;
	for(lp i=1;i<=cnt;i++)
	if(p[i]!=1){
		lp l=st(p[i],tk[top]);
		if(l!=tk[top]){
			while(dfn[l]<dfn[tk[top-1]])link(tk[top],tk[top-1]),top--;
			if(l!=tk[top-1])e[l].clear(),link(l,tk[top]),tk[top]=l;
			else link(l,tk[top--]);
		}
		e[p[i]].clear(),tk[++top]=p[i];
	}for(lp i=1;i<top;i++)link(tk[i],tk[i+1]);
}
int main(){
	n=read();
	for(lp i=1;i<=n-1;i++){lp x=read(),y=read();add(x,y);add(y,x);}
	dfs(1,1);pre();q=read();
	while(q--){
		cnt=read(),flag=0;
		for(lp i=1;i<=cnt;i++){p[i]=read();pd[p[i]]=1;}
		for(lp i=1;i<=cnt;i++){
			for(lp j=head[p[i]];j;j=v[j].ne){
				if(pd[v[j].to]==1){port(-1,'\n');flag=1;break;}
			}
			if(flag==1)break;
		}
		if(flag==1){for(lp i=1;i<=cnt;i++)pd[p[i]]=0;continue;}
		sort(p+1,p+1+cnt,cmp);insert();
		port(dp(tk[1],0),'\n');
		for(lp i=1;i<=cnt;i++)pd[p[i]]=0;
	}
return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;
struct E{
	int to;
	int next;
}ed[maxn*2];
int head[maxn];
int tot;
void J(int a,int b){
	ed[++tot].to=b;
	ed[tot].next=head[a];
	head[a]=tot;
}
int dfn[maxn];
int cnt;
int deep[maxn];
int t[maxn][21];
void Dfs(int x,int fa){
	t[x][0]=fa;
	dfn[x]=++cnt;
	deep[x]=deep[fa]+1;
	for(int i=1;i<=20;i++)
		t[x][i]=t[t[x][i-1]][i-1];
	for(int i=head[x];i;i=ed[i].next){
		if(ed[i].to==fa)
			continue;
		Dfs(ed[i].to,x);
	}
}
int lca(int x,int y){
	if(deep[x]<deep[y])
		swap(x,y);
	int k=deep[x]-deep[y];
	int id=0;
	while(k){
		if(k&1)
			x=t[x][id];
		id++;
		k>>=1;
	}
	if(x==y)
		return x;
	for(int i=20;i>=0;i--)
		if(t[x][i]!=t[y][i]){
			x=t[x][i];
			y=t[y][i];
		}
	return t[x][0];
}
int vis[maxn];
int jl[maxn];
bool cm(int a,int b){
	return dfn[a]<dfn[b];
}
int sta[maxn];
int top;
int root;
int Insert(int x){
	if(!top){
		sta[++top]=x;
		if(deep[x]<deep[root])
			root=x;
		vis[x]=0;
		head[x]=0;
		return 1;
	}
	int lc=lca(x,sta[top]);
	if(lc==sta[top]){
		head[x]=0;
		sta[++top]=x;
		vis[x]=0;
		return 1;
	}
	while(top>=2&&deep[sta[top-1]]>=deep[lc]){
		J(sta[top-1],sta[top]);
		top--;
	}
	if(sta[top]!=lc){
		if(deep[lc]<deep[root])
			root=lc;
		head[lc]=0;
		J(lc,sta[top]);
		sta[top]=lc;
		vis[lc]=1;
	}
	sta[++top]=x;
	head[x]=0;
	vis[x]=0;
	return 1;
}
int bj;
int dp[maxn][2];
void Dfs_(int x){
	if(!vis[x]){
		dp[x][1]=1e7;
		dp[x][0]=0;
		for(int i=head[x];i;i=ed[i].next){
			Dfs_(ed[i].to);
			if(deep[ed[i].to]-deep[x]==1)
				dp[x][0]+=dp[ed[i].to][1];
			else
				dp[x][0]+=min(dp[ed[i].to][1],dp[ed[i].to][0]+1);
			int inf=1e7;
			dp[x][0]=min(dp[x][0],inf);
		}
	}
	else{
		dp[x][0]=0;
		dp[x][1]=0;
		int ans1=0;
		int ans2=1;
		int ans3=0;
		for(int i=head[x];i;i=ed[i].next){
			Dfs_(ed[i].to);
			ans1+=dp[ed[i].to][1];
			ans2+=min(dp[ed[i].to][1],dp[ed[i].to][0]);
			ans3=max(ans3,dp[ed[i].to][1]-dp[ed[i].to][0]);
		}
		if(ans3>0)
			dp[x][0]=ans1-ans3;
		else
			dp[x][0]=ans1;
		dp[x][1]=min(ans2,ans1);
		int inf=1e7;
		dp[x][1]=min(dp[x][1],inf);
		dp[x][0]=min(dp[x][0],inf);
	}
}
int main(){
	int n;
	scanf("%d",&n);
	int js1,js2;
	for(int i=1;i<=n-1;i++){
		scanf("%d%d",&js1,&js2);
		J(js1,js2);
		J(js2,js1);
	}
	int t;
	scanf("%d",&t);
	Dfs(1,0);
	int js;
	memset(head,0,sizeof(head));
	for(int i=1;i<=t;i++){
		scanf("%d",&js);
		top=0;
		deep[0]=1e9;
		root=0;
		tot=0;
		bj=0;
		for(int j=1;j<=js;j++)
			scanf("%d",&jl[j]);
		sort(jl+1,jl+1+js,cm);
		for(int j=1;j<=js;j++)
			if(!Insert(jl[j])){
				bj=1;
				break;
			}
		while(top>=2){
			J(sta[top-1],sta[top]);
			top--;
		}
		Dfs_(root);
		if(dp[root][0]>=1e7-1&&dp[root][1]>=1e7-1)
			printf("-1\n");
		else
			printf("%d\n",min(dp[root][0],dp[root][1]));
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

#define ll long long

const int maxn=1e5+10;
const int maxlg=2e1;
const ll inf=1e6+10;

struct Node
{
	int to,next;
}e[maxn<<1];
int head[maxn];
int fa[maxn][maxlg];
bool vis[maxn];
int deep[maxn];
int a[maxn];
int dfn[maxn];
int sta[maxn];
ll dp[maxn][2];
bool go_home;
int t_cnt,cnt,top,num,rt,ans;
vector<int> vec[maxn];

void add(int x,int y)
{
	e[++cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}

void dfs(int x)
{
	dfn[x]=++t_cnt;
	for(int i=1;i<maxlg;i++)
		fa[x][i]=fa[fa[x][i-1]][i-1];
	for(int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if(v==fa[x][0]) continue;
		fa[v][0]=x,deep[v]=deep[x]+1;
		dfs(v);
	}
}

int cmp(const int &x,const int &y)
{
	return dfn[x]<dfn[y];
}

int query(int x,int y)
{
	if(deep[x]<deep[y]) swap(x,y);
	int now=deep[x]-deep[y];
	for(int i=0;now;i++) if(now&1<<i) now^=1<<i,x=fa[x][i];
	if(x==y) return x;
	for(int i=maxlg-1;i>=0;i--)
		if(fa[x][i]!=fa[y][i])
			x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}

void add_edge(int x,int y)
{
	vec[x].push_back(y);
	if(vis[x]&&vis[y]&&deep[x]+1==deep[y]) go_home=1;
}

void push_sta(int x)
{
	sta[++top]=x;
	if(deep[x]<deep[rt]) rt=x;
}

void insert(int x)
{
	if(!top)
	{
		push_sta(x);
		return;
	}
	int lca=query(x,sta[top]);
	if(sta[top]!=lca)
	{
		while(top>=2&&deep[sta[top-1]]>=deep[lca]) add_edge(sta[top],sta[top--]);
		if(lca!=sta[top]) add_edge(lca,sta[top--]),push_sta(lca);
	}
	push_sta(x);
}

void dfs2(int x)
{
	if(vis[x])
	{
		dp[x][0]=0;
		for(int i=0;i<vec[x].size();i++)
		{
			int v=vec[x][i];
			dfs2(v);
			if(deep[v]==deep[x]+1) dp[x][0]+=dp[v][1];
			else dp[x][0]+=min(dp[v][1],dp[v][0]+1);
		}
	}
	else
	{
		ll ans1=0,ans2=1,ans3=0;
		for(int i=0;i<vec[x].size();i++)
		{
			int v=vec[x][i];
			dfs2(v);
			ans1+=dp[v][1],ans2+=min(dp[v][1],dp[v][0]);
			ans3=max(ans3,dp[v][1]-dp[v][0]);
		}
		dp[x][1]=min(ans1,ans2);dp[x][0]=ans1-ans3;
	}
	dp[x][1]=min(dp[x][1],inf),dp[x][0]=min(dp[x][0],inf);
}

void dfs_clr(int x)
{
	for(int i=0;i<vec[x].size();i++)
	{
		int v=vec[x][i];
		dfs_clr(v);
	}
	vec[x].clear(),vis[x]=0,dp[x][0]=dp[x][1]=inf;
}

int main()
{
	int n,x,y,q,k;
	scanf("%d",&n);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		add(x,y),add(y,x);
	}
	dfs(x);deep[0]=inf;
	for(int i=1;i<=n;i++) dp[i][0]=dp[i][1]=inf;
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d",&k);
		for(int i=1;i<=k;i++) scanf("%d",&a[i]),vis[a[i]]=1;
		sort(a+1,a+k+1,cmp);
		go_home=0;top=rt=num=ans=0;
		for(int i=1;i<=k;i++)
		{
			insert(a[i]);
			if(go_home) break;
		}
		while(top>=2) add_edge(sta[top],sta[top--]);
		dfs2(rt);
		if(go_home) printf("-1\n");
		else printf("%lld\n",min(dp[rt][1],dp[rt][0]));
		dfs_clr(rt);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN=100001;
const int MAXM=200001;
int depth[MAXN],fa[MAXN][18],st[MAXN],num[MAXN],head1[MAXN],wk[MAXN],lg2[MAXN],dp[MAXN],top,n,qn,dfn[MAXN],cnt,ans;
bool que[MAXN];
struct Edge
{
    int from,to,nxt;
}e[MAXM<<1],e1[MAXM<<1];
int head[MAXN],kcnt,kkcnt;
void add(int f,int t)
{
    e[++kcnt].from=f;
    e[kcnt].to=t;
    e[kcnt].nxt=head[f];
    head[f]=kcnt;
    return ;
}
void add1(int f,int t)
{
    e1[++kkcnt].from=f;
    e1[kkcnt].to=t;
    e1[kkcnt].nxt=head1[f];
    head1[f]=kkcnt;
    e1[++kkcnt].from=t;
    e1[kkcnt].to=f;
    e1[kkcnt].nxt=head1[t];
    head1[t]=kkcnt;
    return ;
}
int read(void)
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void dfs1(int u,int f)
{
    dfn[u]=++cnt;
    fa[u][0]=f;
    depth[u]=depth[f]+1;
    for(int k=0;fa[u][k];k++)
        fa[u][k+1]=fa[fa[u][k]][k];
    for(int i=head[u];i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(v==f)
            continue;
        if(!dfn[v])
            dfs1(v,u);
    }
    return ;
}
int LCA(int x,int y)
{
    if(depth[x]<depth[y])
        swap(x,y);
    while(depth[x]>depth[y])
        x=fa[x][lg2[depth[x]-depth[y]]];
    if(x==y)
        return x;
    for(int i=lg2[depth[x]];i>=0;i--)
        if(fa[x][i]&&fa[x][i]!=fa[y][i])
        {
            x=fa[x][i];
            y=fa[y][i];
        }
    return fa[x][0];
}
void insert(int u)
{
    int lca=LCA(st[top],u);
    while(top)
        if(depth[lca]>=depth[st[top-1]])
        {
            if(lca!=st[top])
            {
                add1(lca,st[top]);
                if(lca!=st[top-1])
                    st[top]=lca;
                else
                    --top;
            }
            break;
        }
        else
        {
            add1(st[top-1],st[top]);
            top--;
        }
    st[++top]=u;
}
void dfs2(int u,int f)
{
    dp[u]=que[u];
    int num=0;
    for(int i=head1[u];i;i=e1[i].nxt)
    {
        int v=e1[i].to;
        if(v==f)
            continue;
        dfs2(v,u);
        num+=dp[v];
    }
    if(que[u])
    {
        ans+=num;
        dp[u]=1;
    }
    else if(num>=2)
        ans++,dp[u]=0;
    else
        dp[u]=(num>0);
    head1[u]=0;
    que[u]=0;
    return ;
}
bool cmp(int a,int b)
{
    return dfn[a]<dfn[b];
}
inline void write(long long x){
	if (x < 0) x = ~x + 1, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
int main(void)
{
    n=read();
    for(int i=1;i<n;i++)
    {
        int u=read(),v=read();
        add(u,v);
        add(v,u);
    }
    dfs1(1,0);
    qn=read();
    while(qn--)
    {
        ans=top=kkcnt=0;
        bool flag=false;
        int num=read();
        for(int i=1;i<=num;i++)
            wk[i]=read(),que[wk[i]]=true;
        for(int i=1;i<=num;i++)
            if(que[fa[wk[i]][0]])
            {
                putchar('-');
                putchar('1');
                putchar('\n');
                flag=true;
                break;
            }
        if(flag)
        {
            for(int i=1;i<=num;i++)
                que[wk[i]]=false;
            continue;
        }
        sort(wk+1,wk+1+num,cmp);
        st[++top]=wk[1];
        for(int i=2;i<=num;i++)
            insert(wk[i]);
        while(--top)
            add1(st[top],st[top+1]);
        dfs2(st[1],0);
        if(ans==0)
        {
            putchar('-');
            putchar('1');
            putchar('\n');
            continue;
        }
        write(ans);
        putchar('\n');
    }
}


#pragma GCC diagnostic error "-std=c++11"
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 10;
int n, m, k, cnt;
int a[MAXN], head1[MAXN], head2[MAXN], f[MAXN], g[MAXN], key[MAXN];
struct Edge {int x, y, nxt;}e[MAXN << 1];

inline int read() {
	int x = 0, flag = 1;
	char ch = getchar();
	while(ch < '0' or ch > '9') {if(ch == '-')	flag = -1; ch = getchar();}
	while(ch >= '0' and ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
	return x * flag;
}

inline void add_edge1(int x, int y) {
	e[++cnt].x = x, e[cnt].y = y;
	e[cnt].nxt = head1[x], head1[x] = cnt;
}

inline void add_edge2(int x, int y) {
	e[++cnt].x = x, e[cnt].y = y;
	e[cnt].nxt = head2[x], head2[x] = cnt;
}

namespace Tree {
	int cnt1;
	int deep[MAXN], size[MAXN], top[MAXN], dfn[MAXN], son[MAXN], fa[MAXN];
	inline void dfs1(int x) {
		dfn[x] = ++cnt1, size[x] = 1;
		for(register int i = head1[x]; ~i; i = e[i].nxt) {
			int to = e[i].y;
			if(to == fa[x])	continue;
			fa[to] = x, deep[to] = deep[x] + 1;
			dfs1(to);	size[x] += size[to];
			if(size[to] > size[son[x]])	size[x] = to;
		}
	}
	inline void dfs2(int x, int tp) {
		top[x] = tp;
		if(son[x])	dfs2(son[x], tp);
		for(register int i = head1[x]; ~i; i = e[i].nxt) {
			int to = e[i].y;
			if(to == fa[x] or to == son[x])
				continue;
			dfs2(to, to);
		}
	}
	inline int lca(int x, int y) {
		while(top[x] != top[y]) {
			if(deep[top[x]] < deep[top[y]])
				x ^= y ^= x ^= y;
			x = fa[top[x]];
		}
		return deep[x] < deep[y] ? x : y;
	}
	inline bool cmp(int x, int y) {
		return dfn[x] < dfn[y];
	}
	inline void build() {
		static int st[MAXN], cnt2 = 0;
		cnt1 = 0;
		sort(a + 1, a + k + 1, cmp);
		for(register int i = 1; i <= k; i++)
			if(a[i] != a[i - 1])	st[++cnt2] = a[i];
		k = cnt2;
		for(register int i = 1; i <= k; i++)
			a[i] = st[i];
		cnt2 = 1, st[1] = 1, head2[1] = -1;
		for(register int i = 1; i <= k; i++) {
			if(a[i] == 1)	continue;
			int num_lca = lca(a[i], st[cnt2]);
			if(num_lca != st[cnt2]) {
				while(dfn[num_lca] < dfn[st[cnt2 - 1]]) {
					int u = st[cnt2], v = st[cnt2 - 1];
					add_edge2(u, v), add_edge2(v, u);
					cnt2--;
				}
				if(dfn[num_lca] > dfn[st[cnt2 - 1]]) {
					head2[num_lca] = -1;
					add_edge2(st[cnt2], num_lca), add_edge2(num_lca, st[cnt2]);
					st[cnt2] = num_lca;
				}else {
					add_edge2(num_lca, st[cnt2]), add_edge2(st[cnt2--], num_lca);
				}
			}
			head2[a[i]] = -1, st[++cnt2] = a[i];
		}
		for(register int i = 1; i < cnt2; i++)
			add_edge2(st[i], st[i + 1]), add_edge2(st[i + 1], st[i]);
	}
}

inline void init(int x, int fa) {
	f[x] = 0, g[x] = 0;
	int num = 0;
	for(register int i = head2[x]; ~i; i = e[i].nxt) {
		if(e[i].y == fa)	continue;
		init(e[i].y, x);
		f[x] += f[e[i].y], num += g[e[i].y];
	}
	if(key[x])	g[x] = 1, f[x] += num;
	else if(num > 1)	g[x] = 0, ++f[x];
		 else g[x] = num;
}

signed main() {
	n = read();
	memset(head1, -1, sizeof(head1));
	memset(head2, -1, sizeof(head2));
	for(register int i = 1; i < n; i++) {
		int x = read(), y = read();
		add_edge1(x, y), add_edge1(y, x);
	}
	Tree::dfs1(1), Tree::dfs2(1, 1), m = read();
	while(m--) {
		k = read();
		for(register int i = 1; i <= k; i++)
			key[a[i] = read()] = 1;
		bool flag = true;
		for(register int i = 1; i <= k and flag; i++)
			if(key[Tree::fa[a[i]]])	flag = false;
		if(!flag) {
			printf("-1\n");
		}else {
			Tree::build(), init(1, 0);
			printf("%d\n", f[1]);
		}
		for(register int i = 1; i <= k; i++)
			key[a[i]] = 0;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;
typedef long long LL;
const int N = 3e5 + 5;
int read() {
	int x = 0, f = 1; char ch;
	while(! isdigit(ch = getchar())) (ch == '-') && (f = -f);
	for(x = ch^48; isdigit(ch = getchar()); x = (x<<3) + (x<<1) + (ch^48));
	return x * f;
}
template <class T> T Max(T a, T b) { return a > b ? a : b; }
template <class T> T Min(T a, T b) { return a < b ? a : b; }
int n, m;
struct Edge {
	int to;
	Edge* nxt;
	Edge(int to, Edge* nxt) : to(to), nxt(nxt) {}
} *head[N];
void add(int x, int y) {
	head[x] = new Edge(y, head[x]);
}
vector<int> v[N];
void Add(int x, int y) { v[x].push_back(y); }
int a[N], dfn[N], Top[N], siz[N], son[N], sta[N];
int top, dep[N], fa[N], tot, ans, tag[N];
int cop(int x, int y) { return dfn[x] < dfn[y]; }
void dfs1(int x, int f) {
	dep[x] = dep[fa[x] = f] + (siz[x] = 1);
	for(Edge* i = head[x]; i; i = i->nxt) {
		if(i->to == f) continue;
		dfs1(i->to, x);
		siz[x] += siz[i->to];
		if(siz[i->to] > siz[son[x]]) son[x] = i->to;
	}
}
void dfs2(int x, int tp) {
	Top[x] = tp; dfn[x] = ++ tot;
	if(!son[x]) return;
	dfs2(son[x], tp);
	for(Edge* i = head[x]; i; i = i->nxt) {
		if(Top[i->to] == 0) {
			dfs2(i->to, i->to);
		}
	}
}
int LCA(int x, int y) {
	while(Top[x] != Top[y]) {
		if(dep[Top[x]] < dep[Top[y]]) swap(x, y);
		x = fa[Top[x]];
	}
	return dep[x] < dep[y] ? x : y;
}
void Build_Virtual_Tree (int k) {
	sort(a + 1, a + k + 1, cop);
	if(a[1] != 1) sta[top = 1] = 1;
	for(int i = 1; i <= k; ++ i) {
		int x = a[i];
		if(top == 0) { sta[top = 1] = x; continue; }
		int lca = LCA(sta[top], x);
		while((top > 1) && (dep[lca] < dep[sta[top - 1]])) Add(sta[top - 1], sta[top]), top --;
		if(dep[lca] < dep[sta[top]]) Add(lca, sta[top --]);
		if((top == 0) || (sta[top] != lca)) sta[++ top] = lca;
		sta[++ top] = x;
	}
	if(top) while(-- top) Add(sta[top], sta[top + 1]);
//	sta[top = 1] = 1;
//	sort(a + 1, a + k + 1, cop);
//	for(int i = 1; i <= k; ++ i) {
//		int x = a[i];
//		int lca = LCA(x, sta[top]);
//		if(lca == sta[top]) continue;
//		while(top > 1 && dfn[sta[top - 1]] >= dfn[lca]) Add(sta[top - 1], sta[top]), top --;
//		if(lca != sta[top]) Add(lca, sta[top]), sta[top] = lca;
//		sta[++ top] = x;
//	}
//	while(top > 0)  Add(sta[top - 1], sta[top]), top--;
}
void DP(int x) {
	if(tag[x]) {
		for(int i = 0; i < (int)v[x].size(); ++ i) {
			int to = v[x][i]; DP(to);
			if(tag[to]) tag[to] = 0, ans ++;
		}
	}
	else {
		for(int i = 0; i < (int)v[x].size(); ++ i) {
			int to = v[x][i]; DP(to);
			tag[x] += tag[to];
			tag[to] = 0;
		}
		if(tag[x] > 1) tag[x] = 0, ans ++;
	}
	v[x].clear();
}
signed main() {
	n = read();
	for(int i = 1, x, y, z; i < n; ++ i) {
		x = read(); y = read();
		add(x, y); add(y, x);
	}
	dfs1(1, 0); dfs2(1, 1);
	m = read();
	while(m --> 0) {
		int k = read();
		for(int i = 1; i <= k; ++ i) tag[a[i] = read()] = 1;
		int flag = 0;
		for(int i = 1; i <= k; ++ i) {
			if(tag[fa[a[i]]]) { puts("-1"); flag = 1; break; }
		}
		if(flag == 1) {
			for(int i = 1; i <= k; ++ i) tag[a[i]] = 0;
			continue;
		}
		ans = 0;
		Build_Virtual_Tree(k);
		DP(1);
		printf("%lld\n", ans);
		tag[1] = 0;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#define ri register
#define inf 0x7fffffff
#define E (1)
#define mk make_pair
using namespace std; const int N=200010, M=19;
inline int read()
{
	int s=0, w=1; ri char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') w=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar(); return s*w;
}
void print(int x) {if(x<0) x=-x, putchar('-'); if(x>9) print(x/10); putchar(x%10+'0'); }
int n,q,f[N][M+1],d[N],id[N],nowid;
int K,c[N],book[N],sta[N],tp,dp[N];
int head[N],maxE; struct Edge{int nxt,to; }e[N];
inline void Add(int u,int v) {e[++maxE].nxt=head[u]; head[u]=maxE; e[maxE].to=v; }
inline bool cp(int x,int y) { return id[x]<id[y]; }
void DFS(int x,int fa)
{
	d[x]=d[fa]+1, f[x][0]=fa, id[x]=++nowid;
	for(ri int i=1;i<=M;i++) f[x][i]=f[f[x][i-1]][i-1];
	for(int i=head[x];i;i=e[i].nxt) if(e[i].to^fa) DFS(e[i].to,x);
}
inline int LCA(int x,int y)
{
	if(d[x]>d[y]) swap(x,y);
	for(ri int i=M;~i;i--) if(d[f[y][i]]>=d[x]) y=f[y][i];
	if(x==y) return x;
	for(ri int i=M;~i;i--) if(f[x][i]^f[y][i]) x=f[x][i], y=f[y][i];
	return f[x][0];
}
int Solve(int x)
{
	int res,gv; res=gv=0;
	for(int i=head[x];i;i=e[i].nxt) res+=Solve(e[i].to), gv+=dp[e[i].to];
	if(book[x]) dp[x]=1, res+=gv;
	else if(gv>1) dp[x]=0, res++;
	else dp[x]=gv;
	return res;
}
inline void Clean() { for(ri int i=1;i<=K;i++) book[c[i]]=0; }
signed main()
{
	n=read();
	for(ri int i=1;i<n;i++)
	{
		int u=read(), v=read();
		Add(u,v), Add(v,u);
	}
	DFS(1,0);
	for(q=read();q;Clean(), q--)
	{
		K=read(); bool fgg=0;
		for(ri int i=1;i<=K;i++) c[i]=read(), book[c[i]]=1;
		for(ri int i=1;i<=K;i++) if(book[f[c[i]][0]]) { puts("-1"); fgg=1; break; }
		if(fgg) continue;
		sort(c+1,c+1+K,cp);
		sta[tp=1]=1, maxE=head[1]=0;
		for(ri int i=1;i<=K;i++)
		{
			if(c[i]==1) continue;
			int pp=LCA(c[i],sta[tp]);
			if(pp^sta[tp])
			{
				while(id[pp]<id[sta[tp-1]]) Add(sta[tp-1],sta[tp]), tp--;
				if(id[pp]>id[sta[tp-1]]) head[pp]=0, Add(pp,sta[tp]), sta[tp]=pp;
				else Add(pp,sta[tp--]);
			}
			head[c[i]]=0, sta[++tp]=c[i];
		}
		for(ri int i=1;i<tp;i++) Add(sta[i],sta[i+1]);
		printf("%lld\n",Solve(1));
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int gi()
{
	char c;int num=0,flg=1;
	while((c=getchar())<'0'||c>'9')if(c=='-')flg=-1;
	while(c>='0'&&c<='9'){num=num*10+c-48;c=getchar();}
	return num*flg;
}
#define N 100005
int fir[N],to[2*N],nxt[2*N],cnt;
int fa[N],son[N],siz[N],top[N],dep[N],dfn[N],dc;
int b[N],bct,all,stk[N],TP;
int f[N],g[N];
bool vis[N];
const int INF=0x3f3f3f3f;

void adde(int a,int b)
{
	to[++cnt]=b;nxt[cnt]=fir[a];fir[a]=cnt;
	//printf("edge%d:%d %d %d\n",cnt-1,a,b,c);
}
void dfs(int u)
{
	dfn[u]=++dc;siz[u]=1;dep[u]=dep[fa[u]]+1;
	for(int v,p=fir[u];p;p=nxt[p])if((v=to[p])!=fa[u]){
		fa[v]=u;dfs(v);siz[u]+=siz[v];
		if(siz[son[u]]<siz[v])son[u]=v;
	}
}
void pdfs(int u)
{
	if(son[u])top[son[u]]=top[u],pdfs(son[u]);
	for(int v,p=fir[u];p;p=nxt[p])
		if((v=to[p])!=fa[u]&&v!=son[u])
			top[v]=v,pdfs(v);
}
int LCA(int x,int y)
{
	while(top[x]!=top[y]){if(dep[top[x]]<dep[top[y]])swap(x,y);x=fa[top[x]];}
	return dep[x]<dep[y]?x:y;
}
bool cmp(int x,int y){return dfn[x]<dfn[y];}

void DP(int u)
{
	for(int v,p=fir[u];p;p=nxt[p]){
		v=to[p];DP(v);
		if(vis[u]) f[u]+=f[v]+g[v];
		else f[u]+=f[v],g[u]+=g[v];
	}
	if(vis[u])g[u]=1;
	else{if(g[u]>1)f[u]++,g[u]=0;}
}

int main()
{
	int n,m,i,u,v,k;
	n=gi();
	for(i=1;i<n;i++){
		u=gi();v=gi();
		adde(u,v);adde(v,u);
	}
	dfs(1);top[1]=1;pdfs(1);
	memset(fir,0,sizeof(fir));cnt=0;

	m=gi();
	while(m--){
		k=gi();bool flg=0;
		for(i=1;i<=k;i++){b[i]=gi();vis[b[i]]=1;if(b[i]==1)flg=1;}
		if(!flg)b[++k]=1;bct=k;
		flg=0;for(i=1;i<=k;i++)if(vis[b[i]]&&vis[fa[b[i]]]){flg=1;break;}
		if(flg){printf("-1\n");for(i=1;i<=bct;i++)vis[b[i]]=0;continue;}

		sort(b+1,b+k+1,cmp);TP=0;
		stk[++TP]=b[1];
		for(i=2;i<=k;i++){
			int lca=LCA(b[i],stk[TP]);
			if(lca==stk[TP]){stk[++TP]=b[i];continue;}
			while(TP>1&&dep[stk[TP-1]]>=dep[lca]){adde(stk[TP-1],stk[TP]);TP--;}
			if(lca!=stk[TP]){adde(lca,stk[TP]);stk[TP]=lca;b[++bct]=lca;}
			stk[++TP]=b[i];
		}
		while(TP>1)adde(stk[TP-1],stk[TP]),TP--;
		DP(1);printf("%d\n",f[1]);

		cnt=0;for(i=1;i<=bct;i++){fir[b[i]]=vis[b[i]]=g[b[i]]=f[b[i]]=0;}
	}
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N=1e5+2,M=2e5+2;
int a[N],lj[M],nxt[M],fir[N],dfn[N],top[N],hc[N],siz[N],f[N],dep[N],st[N];
int n,m,q,i,x,y,c,bs,tp,ance,ans;
inline void read(int &x)
{
	c=getchar();
	while ((c<48)||(c>57)) c=getchar();
	x=c^48;c=getchar();
	while ((c>=48)&&(c<=57))
	{
		x=x*10+(c^48);
		c=getchar();
	}
}
inline void add(int x,int y)
{
	lj[++bs]=y;
	nxt[bs]=fir[x];
	fir[x]=bs;
}
void dfs1(int x)
{
	siz[x]=1;
	int i;
	for (i=fir[x];i;i=nxt[i]) if (lj[i]!=f[x])
	{
		dep[lj[i]]=dep[f[lj[i]]=x]+1;
		dfs1(lj[i]);
		siz[x]+=siz[lj[i]];
		if (siz[hc[x]]<siz[lj[i]]) hc[x]=lj[i];
	}
}
void dfs2(int x)
{
	dfn[x]=++bs;
	if (hc[x])
	{
		int i;
		top[hc[x]]=top[x];
		dfs2(hc[x]);
		for (i=fir[x];i;i=nxt[i]) if ((lj[i]!=f[x])&&(lj[i]!=hc[x])) dfs2(top[lj[i]]=lj[i]);
	}
}
inline int lca(int x,int y)
{
	while (top[x]!=top[y]) if (dep[top[x]]>dep[top[y]]) x=f[top[x]]; else y=f[top[y]];
	if (dep[x]<dep[y]) return x; return y;
}
void qs(int l,int r)
{
	int i=l,j=r,m=dfn[a[l+r>>1]];
	while (i<=j)
	{
		while (dfn[a[i]]<m) ++i;
		while (dfn[a[j]]>m) --j;
		if (i<=j) swap(a[i++],a[j--]);
	}
	if (i<r) qs(i,r);
	if (l<j) qs(l,j);
}
inline void ins(int x)
{
	if (tp==0)
	{
		st[tp=1]=x;
		return;
	}
	ance=lca(st[tp],x);
	while ((tp>1)&&(dep[ance]<dep[st[tp-1]]))
	{
		add(st[tp-1],st[tp]);
		--tp;
	}
	if (dep[ance]<dep[st[tp]]) add(ance,st[tp--]);
	if ((!tp)||(st[tp]!=ance)) st[++tp]=ance;
	st[++tp]=x;
}
void dfs3(int x)
{
	int i;
	if (siz[x]) for (i=fir[x];i;i=nxt[i])
	{
		dfs3(lj[i]);
		if (siz[lj[i]])
		{
			siz[lj[i]]=0;
			++ans;
		}
	}
	else
	{
		for (i=fir[x];i;i=nxt[i])
		{
			dfs3(lj[i]);
			siz[x]+=siz[lj[i]];
			siz[lj[i]]=0;
		}
		if (siz[x]>1)
		{
			++ans;siz[x]=0;
		}
	}
	fir[x]=0;
}
int main()
{
	read(n);
	for (i=1;i<n;i++)
	{
		read(x);read(y);
		add(x,y);add(y,x);
	}
	bs=0;
	dfs1(dep[1]=1);
	dfs2(top[1]=1);
	memset(fir+1,0,n<<2);
	memset(siz+1,0,n<<2);
	read(q);
	bs=0;
	while (q--)
	{
		x=1;
		read(m);
		for (i=1;i<=m;i++)
		{
			read(a[i]);
			siz[a[i]]=1;
		}
		for (i=1;i<=m;i++) if (siz[f[a[i]]])
		{
			puts("-1");
			x=0;
			break;
		}
		if (!x)
		{
			while (m) siz[a[m--]]=0;
			continue;
		}
		ans=0;
		qs(1,m);
		if (a[1]!=1) st[tp=1]=1;
		for (i=1;i<=m;i++) ins(a[i]);
		if (tp) while (--tp) add(st[tp],st[tp+1]);
		dfs3(1);
		siz[1]=bs=0;
		printf("%d\n",ans);
	}
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N=1e5+2,M=2e5+2;
int a[N],lj[M],nxt[M],fir[N],dfn[N],top[N],hc[N],siz[N],f[N],dep[N],st[N];
int n,m,q,i,x,y,c,bs,tp,ance,ans;
inline void read(int &x)
{
    c=getchar();
    while ((c<48)||(c>57)) c=getchar();
    x=c^48;c=getchar();
    while ((c>=48)&&(c<=57))
    {
        x=x*10+(c^48);
        c=getchar();
    }
}//快读
inline void add(int x,int y)
{
    lj[++bs]=y;
    nxt[bs]=fir[x];
    fir[x]=bs;
}//加单向边
void dfs1(int x)
{
    siz[x]=1;
    int i;
    for (i=fir[x];i;i=nxt[i]) if (lj[i]!=f[x])
    {
        dep[lj[i]]=dep[f[lj[i]]=x]+1;
        dfs1(lj[i]);
        siz[x]+=siz[lj[i]];
        if (siz[hc[x]]<siz[lj[i]]) hc[x]=lj[i];
    }
}//树剖预处理
void dfs2(int x)
{
    dfn[x]=++bs;
    if (hc[x])
    {
        int i;
        top[hc[x]]=top[x];
        dfs2(hc[x]);
        for (i=fir[x];i;i=nxt[i]) if ((lj[i]!=f[x])&&(lj[i]!=hc[x])) dfs2(top[lj[i]]=lj[i]);
    }
}//树剖预处理
inline int lca(int x,int y)
{
    while (top[x]!=top[y]) if (dep[top[x]]>dep[top[y]]) x=f[top[x]]; else y=f[top[y]];
    if (dep[x]<dep[y]) return x; return y;
}//求lca
void qs(int l,int r)//按照dfs序排序
{
    int i=l,j=r,m=dfn[a[l+r>>1]];
    while (i<=j)
    {
        while (dfn[a[i]]<m) ++i;
        while (dfn[a[j]]>m) --j;
        if (i<=j) swap(a[i++],a[j--]);
    }
    if (i<r) qs(i,r);
    if (l<j) qs(l,j);
}
inline void ins(int x)
{
    if (tp==0)
    {
        st[tp=1]=x;
        return;
    }
    ance=lca(st[tp],x);//相当于z
    while ((tp>1)&&(dep[ance]<dep[st[tp-1]]))
    {
        add(st[tp-1],st[tp]);
        --tp;
    }
    if (dep[ance]<dep[st[tp]]) add(ance,st[tp--]);
    if ((!tp)||(st[tp]!=ance)) st[++tp]=ance;
    st[++tp]=x;
}//增量构建
void dfs3(int x)
{
    int i;
    if (siz[x]) for (i=fir[x];i;i=nxt[i])
    {
        dfs3(lj[i]);
        if (siz[lj[i]])
        {
            siz[lj[i]]=0;
            ++ans;
        }
    }
    else
    {
        for (i=fir[x];i;i=nxt[i])
        {
            dfs3(lj[i]);
            siz[x]+=siz[lj[i]];
            siz[lj[i]]=0;
        }
        if (siz[x]>1)
        {
            ++ans;siz[x]=0;
        }
    }
    fir[x]=0;//这里清空
}//对每组询问的解决
int main()
{
    read(n);
    for (i=1;i<n;i++)
    {
        read(x);read(y);
        add(x,y);add(y,x);
    }
    bs=0;
    dfs1(dep[1]=1);
    dfs2(top[1]=1);
    memset(fir+1,0,n<<2);
    memset(siz+1,0,n<<2);
    read(q);
    bs=0;
    while (q--)
    {
        x=1;
        read(m);
        for (i=1;i<=m;i++)
        {
            read(a[i]);
            siz[a[i]]=1;
        }
        for (i=1;i<=m;i++) if (siz[f[a[i]]])
        {
            puts("-1");
            x=0;
            break;
        }//特判无解
        if (!x)
        {
            while (m) siz[a[m--]]=0;//清空打过的标记
            continue;
        }
        ans=0;
        qs(1,m);
        if (a[1]!=1) st[tp=1]=1;//先行添加根节点
        for (i=1;i<=m;i++) ins(a[i]);
        if (tp) while (--tp) add(st[tp],st[tp+1]);//回溯
        dfs3(1);
        siz[1]=bs=0;
        printf("%d\n",ans);
    }
}


#include <bits/stdc++.h>

using namespace std;

inline int read()
{
	int x = 0, ch = '~';

	while(!isdigit(ch)) ch = getchar();
	while(isdigit(ch)) x = (x << 3) + (x << 1), x += ch - '0', ch = getchar();

	return x;
}

const int N = 200010;

int n, m, q, h[N], fa[N], bj[N], cnt[N], size[N], deep[N], top[N], h_son[N];
vector <int > link[N];
vector <int > son[N];

void dfs1(int root, int father)
{
	fa[root] = father, deep[root] = deep[father] + 1, size[root] = 1;

	for(int k = 0; k < (int )link[root].size(); k++)
	{
		int to = link[root][k];
		if(to == father) continue;

		dfs1(to, root), size[root] += size[to];
		h_son[root] = (size[to] > size[h_son[root]]) ? to : h_son[root];
	}
}

void dfs2(int root, int Top)
{
	top[root] = Top, cnt[root] = ++cnt[0];
	if(!h_son[root]) return ;

	dfs2(h_son[root], Top);
	for(int k = 0; k < (int )link[root].size(); k++)
	{
		int to = link[root][k];

		if(to != fa[root] && to != h_son[root])
			dfs2(to, to);
	}
}

bool cmp(const int & a, const int & b)
{
	return cnt[a] < cnt[b];
}

int Tp, s[N], answ;

int get_lca(int u, int v)
{
	int tu = top[u], tv = top[v];
	while(tu != tv)
		if(deep[tu] < deep[tv])
			v = fa[tv], tv = top[v];
		else
			u = fa[tu], tu = top[u];

	if(deep[u] > deep[v]) swap(u, v);
	return u;
}

int insert(int x)
{
	if(!Tp) return s[Tp = 1] = x;

	int lca = get_lca(x, s[Tp]);
	while(Tp > 1 && deep[lca] < deep[s[Tp - 1]])
		son[s[Tp - 1]].push_back(s[Tp]), Tp--;
	if(deep[lca] < deep[s[Tp]]) son[lca].push_back(s[Tp--]);

	if(s[Tp] != lca) s[++Tp] = lca;
	return s[++Tp] = x;
}

void dfs(int wz)
{
	if(!bj[wz])
	{
		int cnt = 0;
		for(int k = 0; k < (int )son[wz].size(); k++)
		{
			dfs(son[wz][k]);
			if(bj[son[wz][k]]) cnt++, bj[son[wz][k]] = 0;
		}

		if(cnt == 1) bj[wz] = 1;
		else answ++;
	}
	else
		for(int k = 0; k < (int )son[wz].size(); k++)
		{
			dfs(son[wz][k]);
			if(bj[son[wz][k]]) bj[son[wz][k]] = 0, answ++;
		}

	son[wz].clear();
}

int work()
{
	for(int i = 1; i <= m; i++)
		if(bj[fa[h[i]]])
			return answ = -1;

	for(int i = 1; i <= m; i++)
		insert(h[i]);
	if (Tp) while (--Tp) son[s[Tp]].push_back(s[Tp + 1]);
	dfs(1);
}

int main()
{
	n = read();
	for(register int k = 1; k < n; k++)
	{
		int u = read(), v = read();
		link[u].push_back(v);
		link[v].push_back(u);
	}

	dfs1(1, 0), dfs2(1, 1);

	q = read();

	for(register int k = 1; k <= q; k++)
	{
		m = read(), answ = 0;
		for(int i = 1; i <= m; i++)
			h[i] = read(), bj[h[i]] = 1;
		sort(h + 1, h + 1 + m);
		if(h[1] != 1) s[Tp = 1] = 1;

		work();
		printf("%d\n", answ);
		for(int i = 1; i <= m; i++)
			bj[h[i]] = 0;
		bj[1] = Tp = 0;
	}

	return 0;
}


#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rint register int
using namespace std;

const int maxn=100086;
const int maxm=maxn<<1;

int head[maxn],to[maxm],Next[maxm],cnt,low[maxn],dfn[maxn],DFN,n,m,vis[maxn];
int g[maxn],f[maxn],fa[maxn],son[maxn],siz[maxn],dep[maxn],top[maxn];
int sta[maxn],p[maxm],k;

inline void add(int x,int y){
    to[++cnt]=y;Next[cnt]=head[x];head[x]=cnt;
    to[++cnt]=x;Next[cnt]=head[y];head[y]=cnt;
}

inline int Read(){
    int x=0;char c=getchar();
    while (!isdigit(c))c=getchar();
    while (isdigit(c)){
        x=(x<<3)+(x<<1)+(c^48);
        c=getchar();
    }
    return x;
}

inline void dfs1(int x,int fat){
	fa[x]=fat,dep[x]=dep[fat]+1;
	siz[x]=1;
	int maxson=-1;
	for (rint i=head[x];i;i=Next[i]){
		int v=to[i];
		if (v==fat)continue;
		dfs1(v,x);
		siz[x]+=siz[v];
		if (siz[v]>maxson)maxson=siz[v],son[x]=v;
	}
}

inline void dfs2(int x,int topf){
	top[x]=topf;dfn[x]=++DFN;
	if (son[x])dfs2(son[x],topf);
	for (rint i=head[x];i;i=Next[i]){
		int v=to[i];
		if (v==son[x] || v==fa[x])continue;
		dfs2(v,v);
	}
	low[x]=DFN;
}

inline void dfs3(int x,int fat){
	for (rint i=head[x];i;i=Next[i]){
		int v=to[i];
		if (v==fat)continue;
		dfs3(v,x);
		f[x]+=f[v];
		g[x]+=g[v];
	}
	if (vis[x])f[x]+=g[x],g[x]=1;
	else f[x]+=(g[x]>1),g[x]=(g[x]==1);
}

inline int dp(int rt){
	for (rint i=1;i<=k;++i)if (vis[fa[p[i]]] && vis[p[i]])return -1;
	dfs3(rt,0);
	return f[rt];
}

inline int getlca(int x,int y){
	while (top[x]^top[y]){
		dep[top[x]]<dep[top[y]] ? y=fa[top[y]]:x=fa[top[x]];
	}
	return dep[x]<dep[y] ? x: y;
}

inline int cmp(int x,int y){
	return dfn[x]<dfn[y];
}

int main(){
	n=Read();
	for (rint i=1;i<n;++i){
		int j=Read(),k=Read();
		add(j,k);
	}

	dfs1(1,0);
	dfs2(1,1);

	m=Read();
	while (m--){
		//memset(vis,0,sizeof(vis))
		memset(head,0,sizeof(head));cnt=0;
		memset(f,0,sizeof(f));memset(g,0,sizeof(g));
		memset(vis,0,sizeof(vis));
		k=Read();
		for (rint i=1;i<=k;++i)p[i]=Read(),vis[p[i]]=Read();
		sort(p+1,p+1+k,cmp);
		for (rint i=k;i>1;--i)p[++k]=getlca(p[i],p[i-1]);
		sort(p+1,p+1+k,cmp);k=unique(p+1,p+1+k)-p-1;
		for (rint i=1,top=0;i<=k;++i){
			while (top && low[sta[top]]<dfn[p[i]])--top;
			add(sta[top],p[i]);sta[++top]=p[i];
		}
		printf("%d\n",dp(p[1]));
	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+10;
int head[N],cnt,dep[N],top[N],f[N],dfn[N],t,son[N],size[N],a[N],s[N],tot,c[N];
bool vis[N];
struct node{
	int next,to;
}e[N<<1];
void add(int x,int y){
	e[++cnt].next=head[x],e[cnt].to=y,head[x]=cnt;
}
bool cmp(int x,int y){
	return dfn[x]<dfn[y];
}
inline int read()
{
    register int x=0,t=1;register char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-')t=-1,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
    return x*t;
}
void dfs1(int v,int fa){
	size[v]=1,dep[v]=dep[fa]+1;
	for(int i=head[v];i;i=e[i].next){
		int u=e[i].to;
		if(u==fa)continue;
		f[u]=v;
		dfs1(u,v);
		size[u]+=size[v];
		if(size[u]>size[son[v]])son[v]=u;
	}
}
void dfs2(int v,int tp){
	dfn[v]=++t,top[v]=tp;
	if(son[v])dfs2(son[v],tp);
	for(int i=head[v];i;i=e[i].next){
		int u=e[i].to;
		if(u==f[v]||u==son[v])continue;
		dfs2(u,u);
	}
}
int lca(int u,int v){
	while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]])v=f[top[v]];
		else u=f[top[u]];
	}
	if(dep[u]<dep[v])return u;
	return v;
}
void insert(int v){
	if(!tot){
		s[++tot]=v;
		return;
	}
	int p=lca(v,s[tot]);
	if(p==s[tot]){
		s[++tot]=v;
		return;
	}
	while(tot>1&&dfn[s[tot-1]]>=dfn[p]){
		add(s[tot-1],s[tot]);
		tot--;
	}
	if(s[tot]!=p) add(p,s[tot]),s[tot]=p;
	s[++tot]=v;
}
int dp(int v){
	int ans=0;
	c[v]=0;
	for(int i=head[v];i;i=e[i].next){
		int u=e[i].to;
		ans+=dp(u);
		c[v]+=c[u];
	}
	if(vis[v]) ans+=c[v],c[v]=1;
	else if(c[v]>1)ans++,c[v]=0;
	else if(c[v]==1)c[v]=1;
	head[v]=0;
	return ans;
}
int main(){
	int n,i,x,y,m,k;
	scanf("%d",&n);
	for(i=1;i<n;++i){
		x=read(),y=read();
		add(x,y),add(y,x);
	}
	dfs1(1,0),dfs2(1,1);
	scanf("%d",&m);
	memset(head,0,sizeof head);
	while(m--){
		int flag=1;
		cnt=0;
		k=read();
		for(i=1;i<=k;++i)a[i]=read(),vis[a[i]]=1;
		sort(a+1,a+k+1,cmp);
		for(i=1;i<=k;++i){
			if(vis[f[a[i]]]){
				flag=0;
				printf("-1\n");
				break;
			}
		}
		if(!flag){
			for(i=1;i<=k;++i)vis[a[i]]=0;
			continue;
		}
		for(i=1;i<=k;++i)insert(a[i]);
		while(tot) add(s[tot-1],s[tot]),tot--;
		printf("%d\n",dp(s[1]));
		for(i=1;i<=k;++i)vis[a[i]]=0,c[a[i]]=0;
	}
}


#include <bits/stdc++.h>
#define int long long
const int sz=3e5+5;
using namespace std;
inline int read() {
	int a = 0,f = 1;
	char ch = getchar();
	while(!isdigit(ch)) {
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(isdigit(ch)) {
		a=a*10+ch-'0';
		ch=getchar();
	}
	return a*f;
}
struct node {
	int to,nxt;
} e[sz],E[sz];
struct node1 {
	int id,dis;
	bool operator <(const node1 &x) const {
		return dis>x.dis;
	}
};
bool viss[sz];
int m[sz],dp[sz],g[sz],k;
int n,cnt,head[sz],top,stk[sz],dfn[sz],maxd,dis[sz],Head[sz],Cnt,ans;
int dep[sz],f[sz][20],sum,siz[sz],fla;
void add_edge(int x,int y) {
	e[++cnt]=(node) {y,head[x]};
	head[x]=cnt;
}
void add_edgexu(int x,int y) {
	E[++Cnt]=(node) {
		y,Head[x]
	};
	Head[x]=Cnt;
}
bool cmp(int x,int y) {
	return dfn[x]<dfn[y];
}
void dfs(int now,int fa) {
	dep[now]=dep[fa]+1,f[now][0]=fa,dfn[now]=++sum;
	for(int i=1; (1<<i)<dep[now]; i++)
		f[now][i]=f[f[now][i-1]][i-1];
	for(int i=head[now]; i; i=e[i].nxt) {
		if(e[i].to==fa) continue;
		dfs(e[i].to,now);
	}
}
int lca(int x,int y) {
	if(dep[x]<dep[y]) swap(x,y);
	for(int i=maxd; i>=0; i--)
		if(dep[x]-(1<<i)>=dep[y])
			x=f[x][i];
	if(x==y) return x;
	for(int i=maxd; i>=0; i--)
		if(f[x][i]!=f[y][i])
			x=f[x][i],y=f[y][i];
	return f[x][0];
}
void build()
{
	sort(m+1,m+1+k,cmp);
	stk[top=1]=1,Head[1]=0;
	for(int i=1;i<=k;i++)
	{
		if(m[i]==1) continue;
		int LCA=lca(m[i],stk[top]);
		if(LCA==stk[top])
		{
			Head[m[i]]=0;
			stk[++top]=m[i];
	    	continue;
		}
		while(dfn[stk[top-1]]>dfn[LCA])
	    {
	    	add_edgexu(stk[top-1],stk[top]);
	    	top--;
		}
		if(dfn[LCA]>dfn[stk[top-1]])
		{
			Head[LCA]=0;
			add_edgexu(LCA,stk[top]);
			stk[top]=LCA;
        }
        else
			add_edgexu(LCA,stk[top--]);
        Head[m[i]]=0;
		stk[++top]=m[i];
	}
    for(int i=1;i<top;i++)
    	add_edgexu(stk[i],stk[i+1]);
}
void dfs2(int now,int fa)
{
//      cout<<now<<":"<<fa<<endl;
	  g[now]=0;
	  for(int i=Head[now];i;i=E[i].nxt)
	  {
		 if(E[i].to==fa) continue;
		 dfs2(E[i].to,now);
		 if(viss[E[i].to])
		 {
		   g[now]++;
//           if(now==7) cout<<E[i].to<<"ad"<<endl;
		 }
//	     	cout<<now<<" "<<E[i].to<<endl;
	  }
	  if(viss[now])
	  {
	  ans+=g[now];
	  }
	  else if(g[now]>=2&&!viss[now])
	  {
	    ans++;
//        cout<<g[now]<<" "<<now<<endl;
	  }
	  else if(g[now]==1) viss[now]=1;
}
void init()
{
	top=0,Cnt=0,sum=0,ans=0,fla=0;
	memset(viss,0,sizeof(viss));
//    for(int i=1;i<=n;i++) Head[i]=0,siz[i]=0,viss[i]=0,g[i]=0;
}
signed main()
{
      n=read(),maxd=log(n)/log(2)+1;
	  for(int i=1;i<n;i++)
	  {
	   int u=read(),v=read();
	   add_edge(u,v),add_edge(v,u);
	  }
	  dfs(1,1);
//	  cout<<lca(3,8)<<endl;
//	        cout<<lca(3,23)<<endl;
      int q=read();
      while(q--)
      {
	    init();
        k=read();
      	fla=0;
		for(int i=1;i<=k;i++)
      	 	m[i]=read(),viss[m[i]]=1;
//		for(int i=1;i<=n;i++)
//		cout<<f[i][0]<<" ";
//		cout<<endl;
		for(int i=1;i<=k;i++)
      	   if(viss[f[m[i]][0]]&&m[i]!=f[m[i]][0]) {
			 fla=1;
      	     break;
		}
		if(fla){
      	   cout<<-1<<endl;
		   continue;
		}
        build();
		dfs2(1,1);
//		cout<<g[7]<<"ad"<<endl;
		cout<<ans<<endl;
//	    cout<<lca(3,23)<<endl;
	  }
}
/*
24
2 1
2 4
2 3
1 7
7 8
7 9
4 5
4 11
4 6
5 10
6 14
6 13
3 12
12 15
16 3
17 4
18 11
19 5
20 17
21 3
22 16
23 16
24 21
1
6 13 3  23 8 9 19
*/


#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define N 100005
using namespace std;
struct nood{
	int x,y,z;
}q[2*N];
int k[N],f[N],d[N],si[N],er[N],to[N],df[N],a[N],dp[N],st[N];
int s=0,ts=0,da;
inline int r()
{
	char h=getchar();
	int y=0,q=1;
	while(h<'0'||h>'9'){if(h=='-')q=-1;h=getchar();}
	while(h>='0'&&h<='9'){y=y*10+h-'0';h=getchar();}
	return y*q;
}
inline void jr2(int a,int b)
{
	//cout<<"\n\n";
	//cout<<a<<" "<<b<<"\n";
	q[++s]=nood{k[a],b};
	//cout<<q[s].x<<" "<<q[s].y<<"\n";
	k[a]=s;
	//cout<<"\n\n";
}
void dfs1(int x,int fa,int s)
{
	//cout<<x<<"\n";
	si[x]=1;
	f[x]=fa;
	d[x]=s;
	for(int i=k[x];i;i=q[i].x)
	{
		int y=q[i].y;
		//cout<<"  "<<y<<"\n";
		if(y==fa)continue;
		//cout<<"*"<<y<<"\n";
		dfs1(y,x,s+1);
		si[x]+=si[y];
		if(si[y]>si[er[x]])er[x]=y;
	}
}
void dfs2(int x,int t)
{
	df[x]=++s;
	to[x]=t;
	if(!er[x])return;
	dfs2(er[x],t);
	for(int i=k[x];i;i=q[i].x)if(!df[q[i].y])dfs2(q[i].y,q[i].y);
}
inline int lca(int x,int y)
{
	//cout<<x<<" "<<y<<":";
	while(to[x]!=to[y])
	{
		if(d[to[x]]<d[to[y]])swap(x,y);
		x=f[to[x]];
	}
	if(d[x]>d[y])swap(x,y);
	//cout<<x<<"\n";
	return x;
}
bool px(int a,int b)
{
	return df[a]<df[b];
}
inline void jr(int x)
{
	if(!ts)
	{
		st[++ts]=x;
		return;
	}
	int l=lca(x,st[ts]);
	while(ts>1&&d[st[ts-1]]>d[l])jr2(st[ts-1],st[ts]),ts--;
	if(d[st[ts]]>d[l])jr2(l,st[ts--]);
	if((!ts)||(st[ts]!=l))st[++ts]=l;
	st[++ts]=x;
}
void dfs3(int x)
{
	if(dp[x])
	{
		for(int i=k[x];i;i=q[i].x)
		{
			int y=q[i].y;
			dfs3(y);
			if(dp[y])
			{
				dp[y]=0;
				da++;
			}
		}
	}
	else
	{
		for(int i=k[x];i;i=q[i].x)
		{
			int y=q[i].y;
			dfs3(y);
			dp[x]+=dp[y];
			dp[y]=0;
		}
		if(dp[x]>1)da++,dp[x]=0;
	}
	k[x]=0;
}
int main()
{
	int n=r();
	for(int i=1;i<n;i++)
	{
		int x=r(),y=r();
		jr2(x,y);
		jr2(y,x);
	}
	//for(int i=1;i<=n;i++)
	//for(int j=k[i];j;j=q[j].x)cout<<i<<" "<<q[j].y<<"\n";
	dfs1(1,0,1);
	s=0;
	dfs2(1,1);
	//for(int i=1;i<=n;i++)cout<<si[i]<<" "<<er[i]<<" "<<f[i]<<" "<<df[i]<<" "<<to[i]<<"\n";
	memset(k,0,sizeof(k));
	int qe=r();
	while(qe--)
	{
		s=0;
		da=0;
		int m=r();
		bool bo=0;
		for(int i=1;i<=m;i++)a[i]=r(),dp[a[i]]=1;
		for(int i=1;i<=m;i++)
		if(dp[f[a[i]]])
		{
			cout<<"-1\n";
			bo=1;
			break;
		}
		if(bo)
		{
			for(int i=1;i<=m;i++)dp[a[i]]--;
			continue;
		}
		sort(a+1,a+1+m,px);
		for(int i=1;i<=m;i++)jr(a[i]);
		while(ts)jr2(st[ts],st[ts+1]),ts--;
		for(int i=1;i<=m;i++)
		//for(int j=k[a[i]];j;j=q[j].x)cout<<a[i]<<" "<<q[j].y<<"\n";
		dfs3(1);
		dp[1]=0;
		cout<<da<<"\n";
	}
}


#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 200020;
struct edge
{
	int to, nxt;
}e[maxn << 1];
int head[maxn], cnt;
int dep[maxn], par[maxn], size[maxn], id[maxn], top[maxn], son[maxn], rec;
int n, q;
void add(int x, int y)
{
	e[++cnt].to = y;
	e[cnt].nxt = head[x];
	head[x] = cnt;
}
void dfs1(int x)
{
	size[x] = 1;
	dep[x] = dep[par[x]] + 1;
	for (int i = head[x]; i; i = e[i].nxt)
	{
		int t = e[i].to;
		if (t == par[x]) continue;
		par[t] = x;
		dfs1(t);
		size[x] += size[t];
		if (!son[x] || size[son[x]] < size[t]) son[x] = t;
	}
}
void dfs2(int x, int y)
{
	top[x] = y;
	id[x] = ++ncnt, a[ncnt] = b[x];
	if (son[x]) dfs2(son[x], y);
	for (int i = head[x]; i; i = e[i].nxt)
	{
		int t = e[i].to;
		if (t == par[x] || t == son[x]) continue;
		dfs2(t, t);
	}
}
int lca(int x, int y)
{
	while (top[x] != top[y])
	{
		if (dep[top[x]] < dep[top[y]]) swap(x, y);
		x = par[top[x]];
	}
	if (dep[x] > dep[y]) swap(x, y);
	return x;
}
int sta[maxn], f[maxn], g[maxn], a[maxn], vis[maxn], tp;
bool cmp(int x, int y) { return id[x] < id[y]; }
void insrt(int x)
{
	if (tp == 1) { sta[++tp] = x; return; }
	int z = lca(x, sta[tp]);
	if (z == sta[tp]) { sta[++tp] = x; return; }
	while (tp > 1 && id[sta[tp - 1]] > id[z])
		add(sta[tp - 1], sta[tp]), tp--;
	if (z != sta[tp]) add(z, sta[tp]), sta[tp] = z;
	sta[++tp] = x;
}
void build(int m)
{
    cnt=0; sta[tp=1]=1;
    for (int i=1; i<=m; ++i) insrt(a[i]);
    while (tp>=2) add(sta[tp-1],sta[tp]), tp--;
}
void Clear(int x)
{
	if (!vis[x]) return;
	vis[x] = 0, f[x] = g[x] = 0;
	for (int i = head[x]; i; i = e[i].nxt) Clear(e[i].to);
	head[x] = 0;
}
void dp(int x)
{
	f[x] = g[x] = 0;
	for (int i = head[x]; i; i = e[i].nxt)
	{
		int t = e[i].to, dp(t);
		f[x] += f[t], g[x] += g[t];
	}
	if (!vis[x]) f[x] += (g[x] > 1), g[x] = (g[x] == 1);
	else f[x] += g[x], g[x] = 1;
}
int main()
{
	scanf("%d", &n);
	for (int i = 1; i < n; i++)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		add(x, y), add(y, x);
	}
	dfs1(1), dfs2(1, 1);
//	for (int i = 1; i <= n; i++)
//		for (int j = 1; j <= n; j++)
//			printf("%d %d %d\n", i, j, lca(i, j));
	scanf("%d", &q);
	memset(head, 0, sizeof(head));
	while (q--)
	{
		int flag = 1, m;
		scanf("%d", &m);
		for (int i = 1; i <= m; i++) scanf("%d", &a[i]), vis[a[i]] = 1;
		sort(a + 1, a + m + 1, cmp);
		for (int i = 1; i <= m; i++)
			if (vis[par[a[i]]]) flag = 0;
		build(m);
		if (f) dp(1), printf("%d\n", f[1]);
		else printf("-1\n");
		Clear(1);
	}
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+7,INF=1e5+7;
int fir[N],mn[20][N],head[N],nxt[N],to[N],cnt,tot,dfn[N],lg[N],d[N],n;
inline void link(int u,int v){
	to[++cnt]=v,nxt[cnt]=head[u],head[u]=cnt;
}
int Getmin(int a,int b){
	if(fir[a]<=fir[b]) return a;
	return b;
}
void dfs(int x,int fx){
	fir[x]=++tot;dfn[tot]=x;d[x]=d[fx]+1;
	for(int i=head[x];i;i=nxt[i]){
		int y=to[i];
		if(y==fx) continue;
		dfs(y,x),dfn[++tot]=x;
	}
	return ;
}
inline int lca(int a,int b){
	int l=fir[a],r=fir[b];
	if(l>r) swap(l,r);
	int res=Getmin(mn[lg[r-l+1]][l],mn[lg[r-l+1]][r-(1<<lg[r-l+1])+1]);
	return res;
}
void build(){
	dfs(1,0);lg[0]=-1;
	for(int i=1;i<=n<<1;++i) lg[i]=lg[i>>1]+1,mn[0][i]=dfn[i];
	for(int j=1;j<20;++j)
		for(int i=1;i+(1<<j)-1<=n<<1;++i)
			mn[j][i]=Getmin(mn[j-1][i],mn[j-1][i+(1<<j-1)]);
	return ;
}
bool cmp(int a,int b){return fir[a]<=fir[b];}
struct VT{
	int head[N],nxt[N],to[N],cnt,dp[N][2],sta[N],top,st[N];bool in[N];
	inline void link(int u,int v){
		st[++st[0]]=u,st[++st[0]]=v,to[++cnt]=v,nxt[cnt]=head[u],head[u]=cnt;
	}
	void build(vector<int> k){
		sort(k.begin(),k.end(),cmp);
		if(k[0]!=1) sta[top=1]=1;
		for(int i=0;i<k.size();++i){
			int x=k[i];in[x]=1;//cout<<x<<endl;
			if(top<=1) sta[++top]=x;
			else{
				int g=lca(sta[top],x);
				while(top>1&&fir[sta[top-1]]>=fir[g]) link(sta[top-1],sta[top]),--top;
				if(sta[top]!=g) link(g,sta[top]),sta[top]=g;
				sta[++top]=x;
			}
		}
		while(top>1) link(sta[top-1],sta[top]),--top;top=0;
	}
	void dfs(int x){
		if(!head[x]){
			dp[x][1]=0,dp[x][0]=INF;
			return ;
		}
		dp[x][0]=dp[x][1]=0;
		int res=0;
		for(int i=head[x];i;i=nxt[i]){
			int y=to[i];dfs(to[i]);
			if(in[x]) {
				if(!in[y]) dp[x][1]+=dp[y][0];
				else{
					if(d[y]!=d[x]+1) dp[x][1]+=dp[y][1]+1;
					else {dp[x][1]=INF;break;}
				}
			}
			else dp[x][1]=min(dp[x][1]+dp[y][0],dp[x][0]+dp[y][1]),dp[x][0]+=dp[y][0],res+=min(dp[y][0],dp[y][1]);
			dp[x][0]=min(dp[x][0],INF),dp[x][1]=min(dp[x][1],INF);
		}
		if(!in[x]) dp[x][0]=min(dp[x][0],res+1);
		else dp[x][0]=INF;
		return ;
	}
	void clear(){
		while(st[0]) head[st[st[0]]]=in[st[st[0]]]=0,st[0]--;cnt=0;
	}
}T;
int main(){
	cin>>n;
	for(int i=1;i<n;++i){
		int a,b;scanf("%d%d",&a,&b);link(a,b),link(b,a);
	}
	build();
	int m;cin>>m;
	while(m--){
		vector<int> p;int x;
		scanf("%d",&x);p.resize(x);
		for(int i=0;i<x;++i) scanf("%d",&p[i]);
		T.clear();
		T.build(p);
		T.dfs(1);
		if(T.dp[1][0]==INF&&T.dp[1][1]==INF) puts("-1");
		else printf("%d\n",min(T.dp[1][0],T.dp[1][1]));
	}
	return 0;
}


#include <bits/stdc++.h>
const int N = 3e5;
inline int read() {
	int res(0), f(1); char ch(getchar());
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') res = (res << 1) + (res << 3) + (ch ^ 48), ch = getchar();
	return res * f;
}

struct Edge {
	int to;
	Edge *nxt;
	Edge(int to, Edge *nxt) : to(to), nxt(nxt) {}
} *head[N];

int fa[N], dep[N], siz[N], son[N], top[N], a[N], dfn[N], tot, s[N];
bool flag[N];

void dfs(int x, int fa) {
	dep[x] = dep[::fa[x] = fa] + (siz[x] = 1);
	for (Edge *i = head[x]; i; i = i->nxt) {
		if (i->to == fa) continue;
		dfs(i->to, x);
		siz[x] += siz[i->to];
		if (siz[i->to] > siz[son[x]]) son[x] = i->to;
	}
}

void dfs(int x, int fa, int top) {
	::top[x] = top; dfn[x] = ++ tot;
	if (son[x]) dfs(son[x], x, top);
	for (Edge *i = head[x]; i; i = i->nxt) {
		if (i->to == fa || i->to == son[x]) continue;
		dfs(i->to, x, i->to);
	}
}

namespace fuck {
	Edge *head[N];
	int siz[N];
	void add(int u, int v) { head[u] = new Edge(v, head[u]); head[v] = new Edge(u, head[v]); }

	int dfs(int x, int fa) {
		int sum = 0, ans = 0;
		for (Edge *i = head[x]; i; i = i->nxt) {
			if (i->to == fa) continue;
			ans += dfs(i->to, x);
			sum += siz[i->to];
		}
		if (flag[x]) ans += sum, siz[x] = 1;
		else if (sum > 1) siz[x] = 0, ++ ans;
		else siz[x] = sum;
		head[x] = NULL, flag[x] = 0;
		return ans;
	}

}

using fuck::add;

int LCA(int x, int y) {
	while (top[x] != top[y]) {
		if (dep[top[x]] < dep[top[y]]) std::swap(x, y);
		x = fa[top[x]];
	}
	return dep[x] < dep[y] ? x : y;
}

bool cmp(int x, int y) { return dfn[x] < dfn[y]; }

void build() {
	int tot = read(), top = 0, cnt = tot;
	for (int i = 1; i <= tot; ++ i) flag[a[i] = read()] = 1;
	for (int i = 1; i <= tot; ++ i) if (flag[fa[a[i]]]) { for (int j = 1; j <= tot; ++ j) flag[a[j]] = 0; return (void)(puts("-1")); }
	std::sort(a + 1, a + tot + 1, cmp); s[++ top] = 1;
	for (int i = 1; i <= tot; ++ i) {
		int lca = LCA(a[i], s[top]);
		while (dep[lca] < dep[s[top]]) {
			if (dep[lca] >= dep[s[top - 1]]) {
				add(lca, s[top]);
				-- top;
				if (s[top] != lca) s[++ top] = lca;
			} else add(s[top], s[top - 1]), -- top;
		}
		if (s[top] != a[i]) s[++ top] = a[i];
	}
	while (top > 1) add(s[top - 1], s[top]), -- top;
	printf("%d\n", fuck::dfs(1, 0));
}

int main() {
	int n = read();
	for (int i = 1; i < n; ++ i) {
		int u = read(), v = read();
		head[u] = new Edge(v, head[u]);
		head[v] = new Edge(u, head[v]);
	}
	dfs(1, 0);
	dfs(1, 0, 1);
	int m = read();
	for (; m -- > 0;) build();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5,inf=1e9;
struct Link{int st,to,nxt;}l[N<<1];
int n,Q,head[N],tot;
inline int read(){
	int s=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') f=ch=='-'?-1:f,ch=getchar();
	while(ch>='0'&&ch<='9') s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
	return s*f;
}
inline void add(int x,int y){
	l[++tot].to=y;l[tot].st=x;
	l[tot].nxt=head[x];head[x]=tot;
}
inline void Add(int x,int y){
	add(x,y),add(y,x);
}
int d[N],E[N<<1],R[N<<1],st[20][N<<1],lg[N<<1],cnt;
void dfs1(int x,int fa){
	d[x]=d[fa]+1;E[x]=++cnt,R[cnt]=x;
	for(int i=head[x];i;i=l[i].nxt){
		int y=l[i].to;
		if(y==fa) continue;
		dfs1(y,x);R[++cnt]=x;
	}
}
inline void init(){
	for(int i=2;i<=cnt;++i) lg[i]=lg[i>>1]+1;
	for(int i=1;i<=cnt;++i) st[0][i]=R[i];
	for(int i=1;i<20;++i)
		for(int j=1;j+(1<<i)<=cnt;++j)
			st[i][j]=d[st[i-1][j]]<d[st[i-1][j+(1<<i-1)]]?st[i-1][j]:st[i-1][j+(1<<i-1)];
}
inline int Lca(int x,int y){
	if(E[x]>E[y]) swap(x,y);int len=lg[E[y]-E[x]+1];
	return d[st[len][E[x]]]<d[st[len][E[y]-(1<<len)+1]]?st[len][E[x]]:st[len][E[y]-(1<<len)+1];
}
ll f[N][2];
int s[N],is[N],top,lst,jud;
inline bool cmp(int a,int b){
	return E[a]<E[b];
}
inline void build(int len){
	static int st[N];top=0;st[++top]=1;
	sort(s+1,s+len+1,cmp);lst=len;
	for(int i=1;i<=lst;++i) is[s[i]]=1;
	for(int i=1;i<=lst;++i){
		int x=s[i],lca=Lca(x,st[top]);
		if(x==1) continue;
		if(x==st[top]) st[++top]=x;
		else{
			while(top>=2&&d[st[top-1]]>=d[lca]) Add(st[top],st[top-1]),top--;
			if(st[top]!=lca) Add(st[top],lca),st[top]=lca,s[++len]=lca;
			st[++top]=x;
		}
	}
	for(int i=1;i<top;++i) Add(st[i],st[i+1]);

//	puts("+++++++++++++++++++ asas");
//	for(int i=1;i<=lst;++i) cout<<s[i]<<" "<<is[s[i]]<<endl;
//	for(int i=1;i<=tot;++i) cout<<l[i].st<<" "<<l[i].to<<endl;

	//cout<<"asas"<<endl;
}
void dfs(int x,int fa){
	//cout<<x<<" "<<fa<<endl;
	f[x][0]=f[x][1]=0;
	if(is[x]) f[x][0]=inf;
	ll sum0=0,sum1=0,ss=0,mn=inf;
	for(int i=head[x];i;i=l[i].nxt){
		int y=l[i].to;
		if(y==fa) continue;
		dfs(y,x);ss++;
		if(is[x]&&is[y]){
			//cout<<"+++++++++"<<x<<" "<<y<<" "<<d[x]<<" "<<d[y]<<endl;
			if(d[y]-d[x]==1) jud=1;
			else f[y][0]=f[y][1]+1;
		}
		if(is[x]) f[x][1]+=f[y][0];
		if(!is[x]){
			sum0+=f[y][0];
			mn=min(mn,f[y][1]-f[y][0]);
			sum1+=min(f[y][0],f[y][1]);
		}
	}
	head[x]=0;
	if(!ss){
		if(is[x]) f[x][0]=inf;
		return ;
	}
	if(!is[x]){
		f[x][1]=sum0+mn;
		f[x][0]=sum1+1;
		f[x][0]=min(f[x][0],sum0);
	}
	//cout<<x<<" "<<f[x][0]<<" "<<f[x][1]<<endl;
	//head[x]=0;
}
int main(){
	n=read();
	for(int i=1;i<n;++i) Add(read(),read());
	dfs1(1,0);init();Q=read();
	memset(head,0,sizeof head);
	for(int tm=1;tm<=Q;++tm){
		int len=read();tot=0;jud=0;
		for(int i=1;i<=len;++i) s[i]=read();
		build(len);dfs(1,0);

		if(jud) puts("-1");
		else printf("%lld\n",min(f[1][0],f[1][1]));
		for(int i=1;i<=lst;++i) is[s[i]]=0;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define maxn 100003
using namespace std;
inline void setIO(string s)
{
	string in=s+".in", out=s+".out";
	freopen(in.c_str(),"r",stdin);
}
int edges,tim,n;
int hd[maxn], to[maxn<<1], nex[maxn<<1];
inline void add(int u,int v)
{
	nex[++edges]=hd[u],hd[u]=edges,to[edges]=v;
}
int fa[maxn], top[maxn], dfn[maxn], hson[maxn], siz[maxn], dep[maxn];
void dfs1(int u,int ff)
{
	siz[u]=1,fa[u]=ff,dfn[u]=++tim,dep[u]=dep[ff]+1;
	for(int i=hd[u];i;i=nex[i])
	{
		int v=to[i];
		if(v==ff) continue;
		dfs1(v, u);
		siz[u]+=siz[v];
	}
}
void dfs2(int u,int tp)
{
	top[u]=tp;
	if(hson[u]) dfs2(hson[u], tp);
	for(int i=hd[u];i;i=nex[i])
	{
		int v=to[i];
		if(v==fa[u]||v==hson[u]) continue;
		dfs2(v,v);
	}
}
inline int LCA(int x,int y)
{
	while(top[x]!=top[y])
	{
		dep[top[x]] > dep[top[y]] ? x = fa[top[x]] : y = fa[top[y]];
	}
	return dep[x] < dep[y] ? x : y;
}
int tp;
vector<int>G[maxn];
int arr[maxn],mk[maxn],S[maxn],g[maxn],f[maxn];
int cmp(int i,int j)
{
	return dfn[i]<dfn[j];
}
inline void addvir(int u,int v)
{
	G[u].push_back(v);
}
inline void insert(int x)
{
	if(tp<=1) { S[++tp]=x;  return; }
	int lca=LCA(x,S[tp]);
	if(lca==S[tp]) { S[++tp]=x; return; }
	while(tp > 1 && dep[S[tp - 1]] >= dep[lca]) addvir(S[tp - 1], S[tp]), --tp;
	if(S[tp]!=lca) addvir(lca,S[tp]), S[tp]=lca;
	S[++tp]=x;
}
void DP(int x)
{
	int son=0;
	g[x]=f[x]=0;
	for(int i=0;i<G[x].size();++i)
	{
		int v = G[x][i];
		DP(v);
		f[x]+=f[v];
		g[x]+=g[v];
	}
	if(mk[x]) f[x]+=g[x], g[x]=1;
	else f[x]+=(g[x]>1), g[x]=(g[x]==1);
	G[x].clear();
}
inline void work()
{
	int k;
	scanf("%d",&k);
	for(int i=1;i<=k;++i) scanf("%d",&arr[i]), mk[arr[i]]=1;
	sort(arr+1,arr+1+k,cmp);
    for(int i=2;i<=k;++i)
    	if(arr[i-1]==fa[arr[i]])
    	{
    		for(int j=1;j<=k;++j) mk[arr[j]]=0;
    		printf("-1\n");
    		return;
    	}
    tp=0;
    if(arr[1]!=1) S[tp=1]=1;
    for(int i=1;i<=k;++i) insert(arr[i]);
    while(tp > 1) addvir(S[tp - 1], S[tp]), --tp;
    DP(1);
    printf("%d\n",f[1]);
    for(int j=1;j<=k;++j) mk[arr[j]]=0;
}
int main()
{
	// setIO("input");
	scanf("%d",&n);
	for(int i=1,a,b;i<n;++i)
	{
		scanf("%d%d",&a,&b);
		add(a,b), add(b,a);
	}
	dfs1(1,0), dfs2(1,1);
	int Q;
	scanf("%d",&Q);
	for(int i=1;i<=Q;++i) work();
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

#define ll long long

const int maxn=1e5+10;
const int maxlg=2e1;
const ll inf=1e6+10;

struct Node
{
	int to,next;
}e[maxn<<1];
int head[maxn];
int fa[maxn][maxlg];
bool vis[maxn];
int deep[maxn];
int a[maxn];
int dfn[maxn];
int sta[maxn];
ll dp[maxn][2];
bool go_home;
int t_cnt,cnt,top,num,rt,ans;
vector<int> vec[maxn];

void add(int x,int y)
{
	e[++cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}

void dfs(int x)
{
	dfn[x]=++t_cnt;
	for(int i=1;i<maxlg;i++)
		fa[x][i]=fa[fa[x][i-1]][i-1];
	for(int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if(v==fa[x][0]) continue;
		fa[v][0]=x,deep[v]=deep[x]+1;
		dfs(v);
	}
}

int cmp(const int &x,const int &y)
{
	return dfn[x]<dfn[y];
}

int query(int x,int y)
{
	if(deep[x]<deep[y]) swap(x,y);
	int now=deep[x]-deep[y];
	for(int i=0;now;i++) if(now&1<<i) now^=1<<i,x=fa[x][i];
	if(x==y) return x;
	for(int i=maxlg-1;i>=0;i--)
		if(fa[x][i]!=fa[y][i])
			x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}

void add_edge(int x,int y)
{
	vec[x].push_back(y);
	if(vis[x]&&vis[y]&&deep[x]+1==deep[y]) go_home=1;
}

void push_sta(int x)
{
	sta[++top]=x;
	if(deep[x]<deep[rt]) rt=x;
}

void insert(int x)
{
	if(!top)
	{
		push_sta(x);
		return;
	}
	int lca=query(x,sta[top]);
	if(sta[top]!=lca)
	{
		while(top>=2&&deep[sta[top-1]]>=deep[lca]) add_edge(sta[top],sta[top--]);
		if(lca!=sta[top]) add_edge(lca,sta[top--]),push_sta(lca);
	}
	push_sta(x);
}

void dfs2(int x)
{
	dp[x][0]=(vis[x]^1)*inf,dp[x][1]=vis[x]*inf;
	if(vis[x])
	{
		dp[x][0]=0;dp[x][1]=inf;
		for(int i=0;i<vec[x].size();i++)
		{
			int v=vec[x][i];
			dfs2(v);
			dp[x][0]+=min(dp[v][1],dp[v][0]+1);
		}
	}
	else
	{
		ll ans2=0,ans3=0,num=0;
		for(int i=0;i<vec[x].size();i++)
		{
			int v=vec[x][i];
			dfs2(v);if(vis[v]) num++;
			dp[x][1]+=dp[v][1],ans2+=min(dp[v][1],dp[v][0]);
			ans3=max(ans3,dp[v][1]-dp[v][0]);
		}
		dp[x][0]=dp[x][1]-ans3;dp[x][1]=min(dp[x][1],ans2+1);
		if(num>1) dp[x][0]=inf;if(!ans3) dp[x][0]=inf;
	}
}

void dfs_clr(int x)
{
	for(int i=0;i<vec[x].size();i++)
	{
		int v=vec[x][i];
		dfs_clr(v);
	}
	vec[x].clear(),vis[x]=0,dp[x][0]=dp[x][1]=inf;
}

int main()
{
	int n,x,y,q,k;
	scanf("%d",&n);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		add(x,y),add(y,x);
	}
	dfs(x);deep[0]=inf;
	for(int i=1;i<=n;i++) dp[i][0]=dp[i][1]=inf;
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d",&k);
		for(int i=1;i<=k;i++) scanf("%d",&a[i]),vis[a[i]]=1;
		sort(a+1,a+k+1,cmp);
		go_home=0;top=rt=num=ans=0;
		for(int i=1;i<=k;i++)
		{
			insert(a[i]);
			if(go_home) break;
		}
		while(top>=2) add_edge(sta[top],sta[top--]);
		dfs2(rt);
		if(go_home) printf("-1\n");
		else printf("%lld\n",min(dp[rt][1],dp[rt][0]));
		dfs_clr(rt);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int n,q,m,head1[N],head2[N],cnt1,cnt2;
int dfn[N],tot,sta[N],top;
int size[N],fa[N],topf[N],son[N],dep[N];
int f[N],g[N],rt;
bool fl,vis[N];
struct edge{int to,nxt;}e1[N<<1],e2[N<<1];
void add1(int x,int y)
{
	e1[++cnt1].to=y,e1[cnt1].nxt=head1[x],head1[x]=cnt1;
	e1[++cnt1].to=x,e1[cnt1].nxt=head1[y],head1[y]=cnt1;
}
void add2(int x,int y)
{
	e2[++cnt2].to=y,e2[cnt2].nxt=head2[x],head2[x]=cnt2;
//	printf("%d->%d\n",x,y);
}
struct node{int p,t;}c[N];
bool operator < (const node &a,const node &b){return a.t<b.t;}

void dfs1(int x,int ff)
{
	size[x]=1,fa[x]=ff,dep[x]=dep[ff]+1,dfn[x]=++tot;
	for(int i=head1[x];i;i=e1[i].nxt)
	{
		int y=e1[i].to;if(y==ff) continue;
		dfs1(y,x);size[x]+=size[y];
		if(size[y]>size[son[x]]) son[x]=y;
	}
}

void dfs2(int x,int ff)
{
	topf[x]=ff;
	if(son[x]) dfs2(son[x],ff);
	for(int i=head1[x];i;i=e1[i].nxt)
	{
		int y=e1[i].to;if(y==fa[x]||y==son[x]) continue;
		dfs2(y,y);
	}
}

int lca(int x,int y)
{
	while(topf[x]!=topf[y])
	{
		if(dep[topf[x]]<dep[topf[y]]) swap(x,y);
		x=fa[topf[x]];
	}
	return dep[x]<dep[y]?x:y;
}

void clear()
{
	for(int i=0;i<=m;i++)
	{
		head2[i]=f[i]=g[i]=0;
		vis[i]=0;
	}
	cnt2=top=0;sta[1]=0;
	fl=0;
}

void insert(int x)
{
	int t=lca(x,sta[top]);
	while(top>1&&dfn[sta[top-1]]>=dfn[t])
	{
		add2(sta[top-1],sta[top]);
		sta[top--]=0;
	}
	if(t!=sta[top]) add2(t,sta[top]),sta[top]=t;
	sta[++top]=x;
}

void dp(int x,int ff)
{
	for(int i=head2[x];i;i=e2[i].nxt)
	{
		int y=e2[i].to;
		dp(y,x);
		f[x]+=f[y],g[x]+=g[y];
	}
	if(vis[x]) f[x]+=g[x],g[x]=1;
	else f[x]+=(g[x]>1),g[x]=(g[x]==1);
}

int main()
{
//	freopen("D:\\1.txt","r",stdin);
	scanf("%d",&n);
	for(int i=1,x,y;i<n;i++) scanf("%d%d",&x,&y),add1(x,y);
	dfs1(1,0),dfs2(1,1);
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d",&m);
		clear();
		for(int i=1;i<=m;i++)
		{
			int x;scanf("%d",&x);
			c[i].p=x,c[i].t=dfn[x];
			vis[x]=1;
			if(vis[fa[x]]) fl=1;
		}
		if(fl)
		{
			puts("-1");
			continue;
		}
		sort(c+1,c+m+1);rt=c[1].p;
		sta[++top]=rt;
		for(int i=2;i<=m;i++) insert(c[i].p);
		while(top>1)
		{
			add2(sta[top-1],sta[top]);
			top--;
		}
		dp(rt,0);printf("%d\n",f[rt]);
	}
}


#include<bits/stdc++.h>
using namespace std;
int dfn[100010],dep[100010],siz[100010],n,q,ans,yes;
int top[100010],fa[100010],stk[100010],son[100010],tot,tp,id;
int e1[200010],nxt1[200010],head1[100010],cnt1;
int e[200010],nxt[200010],head[100010],cnt;
int imp[100010],a[100010],upt[100010];
void add(int x,int y){
	cnt++;
	e[cnt]=y;
	nxt[cnt]=head[x];
	head[x]=cnt;
}
void dfs1(int x,int f){
	fa[x]=f;
	siz[x]=1;
	dfn[x]=++tot;
	for(int i=head[x];i;i=nxt[i])
	if(e[i]!=f){
		int y=e[i];
		dep[y]=dep[x]+1;
		dfs1(y,x);
		siz[x]+=siz[y];
		if(siz[y]>siz[son[x]])
		son[x]=y;
	}
}
void dfs2(int x,int t){
	top[x]=t;
	if(son[x]==0) return;
	else dfs2(son[x],t);
	for(int i=head[x];i;i=nxt[i])
	if(top[e[i]]==0) dfs2(e[i],e[i]);
}
int lca(int x,int y){
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]) swap(x,y);
		x=fa[top[x]];
	}
	if(dep[x]>dep[y]) swap(x,y);
	return x;
}
void add1(int x,int y){
	//cout<<x<<" "<<y<<endl;
	if(x==y) return;
	if(imp[x]==id&&imp[y]==id&&fa[y]==x) yes=0;
	ans+=(imp[x]==id?1:0);
}
void ins(int x){
	if(tp==1){
		stk[++tp]=x;
		return;
	}
	int l=lca(stk[tp],x);
	//cout<<stk[tp]<<" "<<x<<" lca:"<<l<<endl;
	if(l==stk[tp]){
		stk[++tp]=x;
		return;
	}
	while(tp>1&&dfn[stk[tp-1]]>=dfn[l])
	add1(stk[tp-1],stk[tp]),tp--;
    if(l!=stk[tp]){
    	add1(l,stk[tp]);
		stk[tp]=l;
	}
    stk[++tp]=x;
}
bool cmp(int x,int y){
	return dfn[x]<dfn[y];
}
int main(){
	int x,y,s;
	scanf("%d",&n);
	for(int i=1;i<n;i++){
		scanf("%d%d",&x,&y);
		add(x,y),add(y,x);
	}
	dfs1(1,-1);
	dfs2(1,1);
	scanf("%d",&q);
	for(int i=1;i<=q;i++){ id=i;
		cnt1=0;
		ans=0,yes=1;
		stk[tp=1]=1;
		head1[1]=0;
		scanf("%d",&s);
		for(int j=1;j<=s;j++){
			scanf("%d",&a[j]);
			imp[a[j]]=id;
		}
		sort(a+1,a+s+1,cmp);
		for(int j=1;j<=s;j++) ins(a[j]);
		while(tp>1) add1(stk[tp-1],stk[tp]),tp--;
		if(imp[1]!=id) ans++;
		printf("%d\n",yes?ans:-1);
	}
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN=100001;
const int MAXM=200001;
int depth[MAXN],fa[MAXN][18],st[MAXN],num[MAXN],head1[MAXN],wk[MAXN],lg2[MAXN],dp[MAXN],top,n,qn,dfn[MAXN],cnt,ans;
bool que[MAXN],vis[MAXN];
struct Edge
{
    int from,to,nxt;
}e[MAXM<<1],e1[MAXM<<1];
int head[MAXN],kcnt,kkcnt;
void add(int f,int t)
{
    e[++kcnt].from=f;
    e[kcnt].to=t;
    e[kcnt].nxt=head[f];
    head[f]=kcnt;
    return ;
}
void add1(int f,int t)
{
    e1[++kkcnt].from=f;
    e1[kkcnt].to=t;
    e1[kkcnt].nxt=head1[f];
    head1[f]=kkcnt;
    e1[++kkcnt].from=t;
    e1[kkcnt].to=f;
    e1[kkcnt].nxt=head1[t];
    head1[t]=kkcnt;
    return ;
}
int read(void)
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void dfs1(int u,int f)
{
    dfn[u]=++cnt;
    fa[u][0]=f;
    depth[u]=depth[f]+1;
    for(int k=0;fa[u][k];k++)
        fa[u][k+1]=fa[fa[u][k]][k];
    for(int i=head[u];i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(v==f)
            continue;
        if(!dfn[v])
            dfs1(v,u);
    }
    return ;
}
int LCA(int x,int y)
{
    if(depth[x]<depth[y])
        swap(x,y);
    while(depth[x]>depth[y])
        x=fa[x][lg2[depth[x]-depth[y]]];
    if(x==y)
        return x;
    for(int i=lg2[depth[x]];i>=0;i--)
        if(fa[x][i]&&fa[x][i]!=fa[y][i])
        {
            x=fa[x][i];
            y=fa[y][i];
        }
    return fa[x][0];
}
void insert(int u)
{
    int lca=LCA(st[top],u);
    while(top)
        if(depth[lca]>=depth[st[top-1]])
        {
            if(lca!=st[top])
            {
                add1(lca,st[top]);
                if(!que[lca])
                    vis[lca]=0,dp[lca]=0,que[lca]=0;
                if(lca!=st[top-1])
                    st[top]=lca;
                else
                    --top;
            }
            break;
        }
        else
        {
            add1(st[top-1],st[top]);
            top--;
        }
    st[++top]=u;
}
void dfs2(int u,int f)
{
    vis[u]=true;
    if(f==fa[u][0]&&que[u]&&que[f])
    {
        ans=-1;
        head1[u]=0;
        return ;
    }
    if(ans==-1)
    {
        head1[u]=0;
        return ;
    }
    for(int i=head1[u];i;i=e1[i].nxt)
    {
        int v=e1[i].to;
        if(v==f)
            continue;
        if(!vis[v])
            dfs2(v,u);
        dp[u]+=dp[v];
    }
    if(que[u])
    {
        ans+=dp[u];
        dp[u]=1;
    }
    else if(dp[u]>=2)
        ans++,dp[u]=0;
    head1[u]=0;
    return ;
}
bool cmp(int a,int b)
{
    return dfn[a]<dfn[b];
}
inline void write(long long x){
	if (x < 0) x = ~x + 1, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
int main(void)
{
    n=read();
    for(int i=1;i<n;i++)
    {
        int u=read(),v=read();
        add(u,v);
        add(v,u);
    }
    dfs1(1,0);
    qn=read();
    while(qn--)
    {
        ans=top=kkcnt=0;
        int num=read();
        for(int i=1;i<=num;i++)
            wk[i]=read(),vis[wk[i]]=0,dp[wk[i]]=0,que[wk[i]]=1;
        sort(wk+1,wk+1+num,cmp);
        st[++top]=wk[1];
        for(int i=2;i<=num;i++)
            insert(wk[i]);
        while(--top)
            add1(st[top],st[top+1]);
        dfs2(st[1],0);
        if(ans==0)
        {
            putchar('-');
            putchar('1');
            putchar('\n');
            continue;
        }
        write(ans);
        putchar('\n');
    }
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

int readint(){
	int x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

int n,m,tot,ncnt,top,k;
int dfn[100005],v[200005],nxt[200005],h[100005],f[100005][20],dep[100005],d[100005][2],s[100005],a[100005];
bool b[100005];
vector<int> g[100005];

void addedge(int x,int y){
	v[++tot]=y; nxt[tot]=h[x]; h[x]=tot;
	v[++tot]=x; nxt[tot]=h[y]; h[y]=tot;
}

void dfs1(int u,int fa){
	dfn[u]=++ncnt;
	for(int i=1;i<=18;i++) f[u][i]=f[f[u][i-1]][i-1];
	for(int p=h[u];p;p=nxt[p]){
		if(v[p]==fa) continue;
		f[v[p]][0]=u,dep[v[p]]=dep[u]+1;
		dfs1(v[p],u);
	}
}

bool cmp(int x,int y){return dfn[x]<dfn[y];}

void dfs2(int u){
//	for(auto x:g[u]) cout<<"TEST "<<u<<' '<<x<<endl;
	d[u][0]=d[u][1]=1<<30;
	if(b[u]) d[u][1]=0;
	else d[u][0]=0;
	ll sum1=0,sum2=0;
	for(auto x:g[u]){
		dfs2(x);
		sum1+=d[x][0],sum2+=min(d[x][0],d[x][1]);
		if(b[u]) d[u][1]+=min(d[x][0],d[x][1]+1);
		else d[u][0]+=min(d[x][0],d[x][1]+1);
	}
	if(!b[u]) for(auto x:g[u]) d[u][1]=min((ll)d[u][1],sum1-d[x][0]+d[x][1]);
	if(!b[u]) d[u][0]=min((ll)d[u][0],sum2+1);
//	cout<<"dfs "<<u<<' '<<d[u][0]<<' '<<d[u][1]<<endl;
	g[u].clear();
}

int lca(int x,int y){
	if(dep[x]<dep[y]) swap(x,y);
	for(int i=18;i>=0;i--) if(dep[f[x][i]]>=dep[y]) x=f[x][i];
	if(x==y) return x;
	for(int i=18;i>=0;i--) if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
	return f[x][0];
}

void insert(int x){
	if(!top) return (void)(s[++top]=x);
	int l=lca(x,s[top]);
	if(l==s[top]) return (void)(s[++top]=x);
	while(top>1&&dfn[l]<=dfn[s[top-1]]) g[s[top-1]].push_back(s[top]),top--;
	if(l!=s[top]) g[l].push_back(s[top]),s[top]=l;
	s[++top]=x;
}

int main(){
	n=readint();
	for(int i=1;i<=n-1;i++) addedge(readint(),readint());
	dep[1]=1; dfs1(1,-1);
	m=readint();
	while(m--){
		k=readint();
		for(int i=1;i<=k;i++) a[i]=readint(),b[a[i]]=true;
		bool fl=true;
		for(int i=1;i<=k;i++) if(b[f[a[i]][0]]) fl=false;
		if(!fl){
			printf("-1\n");
			for(int i=1;i<=k;i++) b[a[i]]=false;
			continue;
		}
		sort(a+1,a+k+1,cmp);
		top=0;
		if(a[1]!=1) insert(1);
		for(int i=1;i<=k;i++) insert(a[i]);
		while(top>1) g[s[top-1]].push_back(s[top]),top--;
		dfs2(1);
		printf("%d\n",min(d[1][0],d[1][1]));
		for(int i=1;i<=k;i++) b[a[i]]=false;
	}
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    long long x;
    bool a;
    cin >> n;
    for(int j=0;j<n;j++) {
        cin>>x;
        a=true;
        long long y =  round(sqrt(x));
        if (y*y == x) {
            for (long long i = 2; i < y; i++) {
                if ((x/i)*i == x) {
                    cout << "NO\n";
                    a=false;
                    break;
                }
            }
            if(a) {
                cout << "YES\n";
            }
        } else {
            cout << "NO\n";
        }
    }

    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    long long x;
    bool a;
    cin >> n;
    for(int j=0;j<n;j++) {
        cin>>x;
        a=true;
        long long y =  round(sqrt(x));
        if (x == 1) {
            cout<<"NO\n";
        }
        if (y*y == x) {
            for (long long i = 2; i < y; i++) {
                if ((x/i)*i == x) {
                    cout << "NO\n";
                    a=false;
                    break;
                }
            }
            if(a) {
                cout << "YES\n";
            }
        } else {
            cout << "NO\n";
        }
    }

    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

/*void test() {
    int64_t t,x;
    cin>>x;

    int s=sqrt(x)+0.5;
    cout<<(isPrime(s) ? "Yes\n" : )
}*/
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    long long x;
    bool a;
    cin >> n;
    for(int j=0;j<n;j++) {
        cin>>x;
        a=true;
        long long y =  round(sqrt(x));
        if (x == 1) {
            cout<<"NO\n";
            continue;
        }
        if (y*y == x) {
            for (long long i = 2; i < y; i++) {
                if ((x/i)*i == x) {
                    cout << "NO\n";
                    a=false;
                    break;
                }
            }
            if(a) {
                cout << "YES\n";
            }
        } else {
            cout << "NO\n";
        }
    }

    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    unsigned long long x;
    bool a;
    cin >> n;
    for(int j=0;j<n;j++) {
        cin>>x;
        a=true;
       long double y = sqrt(x);
        if (floor(y) == y) {
            auto z = (long long ) y;
            for (int i = 2; i < sqrt(z); i++) {
                if (z % i == 0) {
                    cout << "NO\n";
                    a=false;
                    break;
                }
            }
            if(a) {
                cout << "YES\n";
            }
        } else {
            cout << "NO\n";
        }
    }

    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    long long x;
    cin >> n;
    for(int j=0;j<n;j++) {
        cin>>x;
        double y = sqrt(x);
        if (floor(y) == y) {
            long z = (long) y;
            for (int i = 2; i < sqrt(y); i++) {
                if (z % i == 0) {
                    cout << "NO\n";
                    continue;
                }
            }
            cout << "YES\n";
        } else {
            cout << "NO\n";
        }
    }

    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    long long x;
    bool a;
    cin >> n;
    for(int j=0;j<n;j++) {
        cin>>x;
        a=true;
        long long y = (long long)(sqrt(x));
        if (y*y == x) {
            for (int i = 2; i < y; i++) {
                if (x % i == 0) {
                    cout << "NO\n";
                    a=false;
                    break;
                }
            }
            if(a) {
                cout << "YES\n";
            }
        } else {
            cout << "NO\n";
        }
    }

    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    long long x;
    bool a;
    cin >> n;
    for(int j=0;j<n;j++) {
        cin>>x;
        a=true;
        double y = sqrt(x);
        if (floor(y) == y) {
            long z = (long) y;
            for (int i = 2; i < sqrt(y); i++) {
                if (z % i == 0) {
                    cout << "NO\n";
                    a=false;
                    break;
                }
            }
            if(a) {
                cout << "YES\n";
            }
        } else {
            cout << "NO\n";
        }
    }

    return 0;
}