#include<bits/stdc++.h>
using namespace std;
int c,t,n,k,x,y,cnt,head[200005],z[200005],f[200005];
const int mod=1e9+7;
long long ans,jx[100005];
bool v[200005],bz[200005];
struct edge{
	int to,nextt,who;
}edge[200005];
void addedge(int x,int y)
{
	cnt++;
	edge[cnt].who=x;
	edge[cnt].to=y;
	edge[cnt].nextt=head[x];
	head[x]=cnt;
}
int solve(int x)
{
	cout<<edge[x].who<<" "<<edge[x].to<<endl;
	long long sum=0;
	v[x]=true;
	for (int i=head[edge[x].who];i;i=edge[i].nextt)
	{
//		int u=edge[i].to;
		if (v[i]==false)
		{
			sum+=solve(i);
		}
		bz[i]=true;
	}
	for (int i=head[edge[x].to];i;i=edge[i].nextt)
	{
		if (v[i]==false)
		{
			sum+=solve(i);
		}
	}
	if (sum==0) sum=1;
	if (sum>1000000000000)
	{
		sum%=mod;
	}
	v[x]=true;
	return sum;
}
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	cin>>c>>t;
	jx[0]=1;
	for (int i=1;i<=100000;i++)
	{
		jx[i]=jx[i-1]*i;
		jx[i]%=mod;
	}
	while (t--)
	{
		cnt=0;ans=0;
		cin>>n>>k;
		if (c==18)
		{
			cout<<1<<endl;
			continue;
		}
		if (c>=19&&c<=21)
		{
			ans=1;
			cout<<n<<' '<<k<<endl;
			for (int i=1;i<n;i++)
			{
				scanf("%d%d",&x,&y);
			}
			for (int i=1;i<=k;i++)
			{
				scanf("%d",&x);
			}
			ans*=jx[n-2];
			cout<<ans%mod<<endl;
			continue;
		}
		for (int i=1;i<=n;i++)
		{
			head[i]=0;
//			z[i]=0;
		}
		for (int i=1;i<n;i++)
		{
			scanf("%d%d",&x,&y);
			addedge(x,y);
			addedge(y,x);
//			z[x]++;
//			z[y]++;
		}
		
		for (int i=1;i<=k;i++)
		{
			scanf("%d",&x);
//			for (int j=1;j<=n;j++)
//			{
//				f[j]=z[j];
//			}
			ans+=solve(x);
		}
		cout<<ans%mod<<endl;
	}
	return 0;
}
