#include<iostream>
#include<cstring>
#include<fstream>
#include<vector>
using namespace std;
const int maxn=1e5+10;
const int mod=1e9+7;
int c,T;
int n,k,ans;
vector<int>G[20005];
bool f[20005][20005];
bool vis[maxn];
int p[maxn];
struct node{
	int u,v;
}tr[maxn];
int read(){
	int res=0;char ch=getchar();
	while(ch<'0'||ch>'9')ch=getchar();
	while(ch>='0'&&ch<='9'){
		res=(res<<1)+(res<<3)+ch-'0';
		ch=getchar();
	}
	return res;
}
void dfs(int u,int d,bool is){
	if(d==n-1){
		if(is)ans++;
		return;
	}
	vis[u]=1;
	for(int i=0;i<G[u].size();i++){
		int v=G[u][i];
		if(!vis[v]){
			if(!f[u][v]){
				f[u][v]=1;
				dfs(v,d+1,1);
			}else{
				f[u][v]=1;
				dfs(v,d+1,is);
			}
		}
	}
	vis[u]=0;
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	cin >> c >> T;
	if(c==18){
		while(T--){
			n=read();k=read();
			memset(p,0,sizeof(p));
			for(int i=1;i<=n-1;i++){
				tr[i].u=read();tr[i].v=read();
			}
			int st;
			for(int i=1;i<=k;i++){
				st=read();
				p[st]=1;
			}
			for(int i=1;i<=n;i++){
				if(p[st])ans+=2;
			}
			cout << ans << endl;
		}
	}
	while(T--){
		memset(f,0,sizeof(f));
		ans=0;
		n=read();k=read();
		for(int i=1;i<=n-1;i++){
			tr[i].u=read();tr[i].v=read();
		}
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++){
				if(tr[i].v==tr[j].u||tr[i].u==tr[j].v||tr[i].u==tr[j].u||tr[i].v==tr[j].v){
					G[i].push_back(j);
					G[j].push_back(i);
				}
			}
		}
		int st;
		for(int i=1;i<=k;i++){
			st=read();
			dfs(st,1,0);
		}
			cout << ans << endl;
		for(int i=1;i<=n;i++){
			while(!G[i].empty())G[i].pop_back();
		}
	}
	return 0;
}
