#include <bits/stdc++.h>
#define int ll
#define fi first
#define endl '\n'
#define il inline
#define se second
#define pb push_back
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
const int inv = 500000004;
int n, k, eid[N], jc[N];
vector<pii> G[N];
vector<int> G2[N], fa;
int vis[N];
map<vector<int>, bool> mp;
void dfs(int u, int f)
{
	fa[u] = f, vis[u] = 1;
	int cnt = 0;
	for (int i = 1; i < n; i++)
		cnt += fa[i] != 0;
	if (cnt == n - 2)
		mp[fa] = 1;
	for (auto v : G2[u])
		if (!vis[v])
			dfs(v, u);
	fa[u] = 0, vis[u] = 0;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	int Cas, T;
	cin >> Cas >> T;
	jc[0] = 1;
	for (int i = 1; i <= 1e5; i++)
		jc[i] = jc[i - 1] * i % mod;
	while (T--)
	{
		if (Cas == 18)
		{
			cout << 1 << endl;
			continue;
		}
		cin >> n >> k;
		for (int i = 1; i <= n; i++) G[i].clear(), G2[i].clear();
		for (int i = 1, u, v; i < n; i++) cin >> u >> v, G[u].pb({v, i}), G[v].pb({u, i});
		for (int i = 1; i <= k; i++) cin >> eid[i];
		if (19 <= Cas && Cas <= 21)
		{
			if (n > 2) cout << (k * jc[n - 2] % mod - k * (k - 1) % mod * inv % mod * jc[n - 3] % mod + mod) % mod << endl;
			else cout << 1 << endl;
			continue;
		}
		for (int i = 1; i <= n; i++)
			for (int j = 0; j < G[i].size(); j++)
				for (int k = j + 1; k < G[i].size(); k++)
					G2[G[i][j].se].pb(G[i][k].se), G2[G[i][k].se].pb(G[i][j].se);
		fa.resize(n + 1);
		for (int i = 1; i <= k; i++) dfs(eid[i], 0);
		cout << mp.size() << endl;
	}
	return 0;
}
