#include <bits/stdc++.h>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
#define ll long long
using namespace std;
const int N = 1e5+5;
const ll mod = 1e9+7;
void read(int &x)
{
	x=0;char c=getchar();int fh=1;
	while(c > '9' or c < '0')
	{
		if(c == '-') fh = -1;
		c = getchar();
	}
	while(c >= '0' and c <= '9')
	{
		x = x * 10 + c - '0';
		c = getchar();
	}
	x *= fh;
	return ;
}
struct node{
	int x,y;
}a[N];
vector<int> V[N],G[N];
int c,T,n,m,b[N];
#define pr pair<int,int>
pr box[N],fox[N];
ll ans;
#define ull unsigned ll
const ull p1 = 19491001;
ull gethash()
{
	ull ans1 = 0;
	for(int i = 1;i <= n-2;i++) fox[i]=box[i];
	sort(fox+1,fox+1+n-2);
	for(int i = 1;i <= n-2;i++)
	{
		ans1 = ans1*p1+fox[i].first;
		ans1 = ans1*p1+fox[i].second;
	}
	return ans1;
}
set<ull> S;
int vis[N],father[N],q;
void dfs(int x,int num)
{
	if(!x) return ;
	vis[x]++;
	if(num == n-1)
	{
		ll res = gethash();
		if(S.find(res)==S.end()) ans++,S.insert(res);
		vis[x]--;
		return ;
	}
	bool fl = 1;
	for(int j : V[x])
	if(!vis[j])
	{
		box[++q]={min(x,j),max(x,j)};
		father[j] = x;
		dfs(j,num+1);
		fl = 0,q--;
	}
	if(fl) dfs(father[x],num);
	vis[x]--;
	return ;
}
int main()
{
	fre(traverse);
	read(c),read(T);
	while(T--)
	{
		S.clear();
		read(n),read(m);
		for(int i = 1;i <= n-1;i++)
			read(a[i].x),read(a[i].y);
		for(int i = 1;i <= n;i++)
			V[i].clear(),G[i].clear();
		for(int i = 1;i <= m;i++) read(b[i]);
		if(c == 18)
		{
			printf("1\n");
			continue;
		}
		for(int i = 1;i <= n-1;i++)
		{
			for(int j : G[a[i].x]) V[i].emplace_back(j),V[j].emplace_back(i);
			for(int j : G[a[i].y]) V[i].emplace_back(j),V[j].emplace_back(i);
			G[a[i].x].emplace_back(i);
			G[a[i].y].emplace_back(i);
		}
		for(int i = 1;i <= m;i++) dfs(b[i],1);
		printf("%lld\n",ans);
	}
	return 0;
}
