//T3 traverse designed by S_Kuroko
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1e5+5,mod=1e9+7;
vector<int> g2[N];
int a[N];
int x[N],y[N];
long long ans=0;
int n,k;
int vis[N];
void dfs(int now,int fa,int cnt)
{
	if(cnt==n-2)
	{
		ans++;
		return ;
	}
	for(int i=0;i<g2[now].size();i++)
	{
		int to=g2[now][i];
		if(to==fa) continue;
		dfs(to,now,cnt+1);
	}
}

int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	int c,T;
	cin>>c>>T;
	while(T--)
	{
		for(int i=0;i<=100000;i++)
		{
			g2[i].clear();
		}
		cin>>n>>k;
		for(int i=1;i<n;i++)
		{
			cin>>x[i]>>y[i];
		}
		for(int i=1;i<=k;i++)
		{
			cin>>a[i];
		}
		if(c==18)
		{
			long long ans=0;
			for(int i=1;i<=k;i++)
			{
				if(a[i]==1||a[i]==n-1)
				{
					ans+=1;
				}
				else ans+=2;
			}
			cout<<ans%mod<<endl;
			continue;
		}
		if(c>=19&&c<=21)
		{
			long long ans=0;
			int num=n-1;
			cout<<(k*2)%mod<<endl;
			continue;
		}
		if(c<=3)
		{
			for(int i=1;i<n;i++)
			{
				for(int j=1;j<n;j++)
				{
//					if(i==2&&j==3) cout<<x[i]<<" "<<y[i]<<" "<<x[j]<<" "<<y[j]<<endl;
					if(i!=j)
					{
						if(x[i]==x[j]||y[i]==y[j]||x[i]==y[j]||x[j]==y[i])
						{
//							cout<<i<<" "<<j<<endl;
							g2[i].push_back(j);
							g2[j].push_back(i);
						}
					}
				}
			}
			dfs(a[1],0,1);
			cout<<ans/2%mod<<endl;
//			bfs(a[1]);
//			cout<<ans%mod<<endl;
			continue;
		}
		cout<<1<<endl;
	}
	return 0;
}

