#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=100005,md=1e9+7;
ll c,T,n,k,f[N],vis[N],cnt,g[N];
vector<ll>v[N],nv[N];
void dfs(ll x){
	if(f[x])return;
	if(cnt==n-2){
		f[x]=1;
		return;
	}
	for(ll y:nv[x]){
		if(vis[y])continue;
		vis[y]=1;
		cnt++;
		dfs(y);
		vis[y]=0;
		cnt--;
		f[x]+=f[y];
		f[x]%=md;
	}
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%lld %lld",&c,&T);
	while(T--){
		scanf("%lld %lld",&n,&k);
		for(ll i=1;i<=n;i++){
			v[i].clear();
		}
		for(ll i=1,x,y;i<n;i++){
			scanf("%lld %lld",&x,&y);
			v[x].push_back(i);
			v[y].push_back(i);
		}
		for(ll i=1;i<=k;i++){
			scanf("%lld",&g[i]);
		}
		if(c==18){
			printf("1\n");
			continue;
		}
		if(19<=c&&c<=21){
			ll ans=1;
			for(ll i=n-2;i>=1;i--){
				ans*=i;
				ans%=md;
			}
			ans*=k;
			ans%=md;
			printf("%lld\n",ans);
			continue;
		}
		for(ll i=1;i<n;i++){
			for(ll j=0,jt=v[i].size();j<jt;j++){
				for(ll k=0;k<j;k++){
					nv[v[i][j]].push_back(v[i][k]);
					nv[v[i][k]].push_back(v[i][j]);
				}
			}
		}
		ll ans=0;
		for(ll i=1,x;i<=k;i++){
			x=g[i];
			for(ll j=1;j<=n;j++)f[j]=vis[j]=0;
			vis[x]=1;
			cnt++;
			dfs(x);
			vis[x]=0;
			cnt--;
			ans=max(ans,f[x]);
		}
		printf("%lld\n",ans);
	}
	return 0;
} 
