#include<bits/stdc++.h>
#define ll long long
#define fo(i,l,r) for(int i=l;i<=r;i++)
#define fd(i,r,l) for(int i=r;i>=l;i--)
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define mod 1000000007
using namespace std;
int _c;
void read(int&x){
	int b=1;
	char c=getchar();
	while(c<'0'||c>'9')b=(c=='-'?-1:b),c=getchar();
	x=0;
	while(c>='0'&&c<='9')x=x*10+(c^48),c=getchar();
	x*=b;
}
void write(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
ll qp(ll x,int y){
	ll ans=1;
	while(y){
		if(y&1)ans=ans*x%mod;
		x=x*x%mod,y>>=1;
	}
	return ans;
}
int n,k,x,y,a[100005];
vector<int>v[100005],e[100005],w[100005];
bool vis[100005];
void D(int x){
	vis[x]=1;
	for(int i:e[x])if(!vis[i])w[x].pb(i),w[i].pb(x),D(i);
}
map<vector<vector<int>>,bool>mp;
void check(int beg){
	fill(vis+1,vis+n,0);
	fo(i,1,n-1)w[i].clear();
	D(beg);
	vector<vector<int>>tmp;
	fo(i,1,n-1)sort(w[i].begin(),w[i].end()),tmp.pb(w[i]);
	mp[tmp]=1;
	fill(vis+1,vis+n,1);
}
void dfs(int x,int st,int fi){
	if(st>=n-2)return check(fi);
	vis[x]=1;
	do{for(int i:e[x])if(!vis[i])dfs(i,st+1,fi);}while(next_permutation(e[x].begin(),e[x].end()));
	vis[x]=0;
}
void solve(){
	read(n),read(k);
	fo(i,1,n)v[i].clear(),e[i].clear();
	fo(i,1,n-1)read(x),read(y),v[x].pb(i),v[y].pb(i);
	fo(i,1,k)read(a[i]);
	if(_c==18)return puts("1"),void();
	fo(i,1,n)fo(j,0,v[i].size()-1)fo(J,j+1,v[i].size()-1)
		e[v[i][j]].pb(v[i][J]),e[v[i][J]].pb(v[i][j]);
	fill(vis+1,vis+n,0);
	fo(i,1,n-1)sort(e[i].begin(),e[i].end());
	fo(i,1,k)dfs(a[i],0,a[i]);
	write(mp.size()),puts("");
}
signed main(){
	freopen("traverse.in","r",stdin),freopen("traverse.out","w",stdout);
	int _T;
	read(_c),read(_T);
	while(_T--)solve();
	return 0;
}

