#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 10; 
const ll mod = 1e9 + 7;
int head[maxn],cnt;
struct node
{
	int to,nxt;
} edge[maxn];
int c,t,n,k,u,v,e[maxn];
void add_edge(int u,int v)
{
	edge[++cnt] = {v,head[u]};
	head[u] = cnt;
	return ;
}
int dep[maxn],maxdep = -1;
void dfs(int u,int fa)
{
	dep[u] = dep[fa] + 1,maxdep = max(maxdep,dep[u]);
	for (int i = head[u];i;i = edge[i].nxt)
	{
		int v = edge[i].to;
		if (v == fa) continue;
		dfs(v,u);
	}
	return ;
}
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%d%d",&c,&t);
	while (t--)
	{
		maxdep = -1;
		scanf("%d%d",&n,&k);
		for (int i = 1;i < n;i++)
		{
			scanf("%d%d",&u,&v);
			add_edge(u,v),add_edge(v,u);
		} 
		for (int i = 1;i <= k;i++)
			scanf("%d",&e[i]);
		dfs(1,0);
		//cout << maxdep << endl;
		if (maxdep == 1)
			printf("%lld\n",(ll)(n * k) % mod);
		else if (maxdep == n)
			printf("%d\n",1);
		else
			printf("%d\n",0);
	}
	return 0;
}
