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

typedef long long LL;
typedef unsigned long long ULL;
typedef __int128 i128;

LL read() {
	LL sum=0,flag=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') flag=-1; c=getchar();}
	while(c>='0'&&c<='9') {sum=sum*10+c-'0'; c=getchar();}
	return sum*flag;
}

const int N=10;
int n,k,ans;
int vis[N];
struct node {
	int l,r;	
}s[N];
struct edge {
	int to,id;
};vector<edge> g[N];

ULL B=13331;

ULL calc(ULL x,ULL y) {
	if(x>y) swap(x,y);
	return x*B*B*B+y*B;
}

ULL calc2(ULL x,ULL y) {
	if(x>y) swap(x,y);
	return x*B+y*B*B*B;
}

map<pair<ULL,ULL>,int> pd;

void dfs(int nd,ULL h1,ULL h2,int cnt) {
	auto t=s[nd];
	int flag=0;
	if(cnt==n-1) {
//		if(!pd.count({h1,h2})) 
		ans++;
//		pd[{h1,h2}]=1;
		return ;
	} 	
	for(auto nxt:g[t.l]) {
		if(vis[nxt.id]) continue;
		flag=1;
		vis[nxt.id]=1;
		ULL c1=calc(nd,nxt.id),c2=calc2(nd,nxt.id);
		dfs(nxt.id,h1+c1,h2+c2,cnt+1);
		vis[nxt.id]=0;
	}
	for(auto nxt:g[t.r]) {
		if(vis[nxt.id]) continue;
		flag=1;
		vis[nxt.id]=1;
		ULL c1=calc(nd,nxt.id),c2=calc2(nd,nxt.id);
		dfs(nxt.id,h1+c1,h2+c2,cnt+1);
		vis[nxt.id]=0;
	}

}

void Sol(){
	n=read(); k=read();
	for(int i=1;i<n;i++) {
		int l=read(),r=read();
		s[i]={l,r};
		g[l].push_back({r,i});
		g[r].push_back({l,i});
	}
	while(k--) {
		int e=read();
		vis[e]=1;
		dfs(e,0,0,1);
		memset(vis,0,sizeof(vis));
	}
	cout<<ans<<endl;
}

void Clear() {
	for(int i=1;i<=n;i++) g[i].clear();
	for(int i=1;i<=n;i++) vis[i]=0;
	ans=0;
	pd.clear();
}

int main() {
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);

	int c=read(),T=read();
	while(T--) {
		Sol();
		Clear();
	}	

	return 0;
}
/*
7 1
5 2
1 2
1 3
2 4
2 5
1 3

1 1
4 1
1 2
2 3
2 4
1


*/
