#include <bits/stdc++.h>
using namespace std;
inline int read() {
	int x = 0, f = 1; char ch = getchar();
	while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar(); }
	while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
	return x * f;
}
#define ll long long
const int N = 1e5 + 10, mod = 1e9 + 7;
int n, cid, k, id[N];
vector<int> e[N];
int dep[N], fa[N], dfn[N], siz[N], tim;
bool vis[N];
map<ll, bool> mp;
int ans;
void dfs(int u, int f, ll now) {
	vis[u] = 1;
	for(auto v : e[u]) {
		if(v == f || vis[v]) continue;
		dfs(v, u, now + (1ll << (u * 5 + v)));
	}
	if(__builtin_popcount(now) == n - 2) {
//		cout<<" "<<now-2<<endl;
		if(!mp.count(now)) mp[now] = 1, ans++;
		vis[u] = 0;
		return;
	}
	vis[u] = 0;
}
int u[N], v[N];
void MAIN() {
	n = read();
	k = read();
	for(int i = 1; i <= n; i++) e[i].clear();
	for(int i = 1; i < n; i++) {
		u[i] = read(), v[i] = read();
//		e[u[i]].push_back(v[i]);
//		e[v[i]].push_back(u[i]);
	}
	for(int i = 1; i <= k; i++)
		id[i] = read();
	if(cid==18) {
		printf("1\n");
		return;
	}
	if(cid>=19&&cid<=21){
		int res = 1;
//		cout<<n<<endl;
//		puts("Q");
		for(int i = 1; i <= n-2; i++)
			res=1ll*res*i%mod;
		int res2 = 0;
		if(n > 3) {
			res2=1;
			for(int i=1;i<=n-3;i++)
				res2 = 1ll * res2 *i % mod;
			res2=1ll*res2*(1ll*k*(k-1)/2%mod)%mod;
		}
		res=1ll*res*k%mod;
		res=(res-res2+mod)%mod;
//		cout << res<<" "<<n<<" "<<k<<endl;
		printf("%d\n", 1ll * res % mod);
		return;
	}
	if(cid<=7){
		for(int i = 1; i <= n-1; i++)
			for(int j = i + 1; j <= n -1; j++)
				if(i!=j&&(u[i]==v[j]||v[i]==u[j]||u[i]==u[j]||v[i]==v[j])){
					e[i].push_back(j);
					e[j].push_back(i);
//					cout<<"ed:"<<i<<" "<<j<<endl;
				}
		mp.clear();
		for(int i = 1; i <= k; i++)
			dfs(id[i], 0, 0);
		printf("%d\n", ans);
	}
}
int main() {
//	system("fc traverse.out traverse10.ans");
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	cid = read(); int T = read();
	while(T--) MAIN();
	return 0;
}
/*
1 1
4 1
1 2
2 3
2 4
1
*/
