#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <set>
using namespace std;
namespace IO {
	inline int Read() {
		int x = 0, k = 0; char ch = getchar();
		while (!isdigit(ch)) k |= (ch == '-'), ch = getchar();
		while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
		return k ? -x : x;
	}
}
const int MaxN = 5E2 + 10;
int c, m, n, T, tot;
int head[MaxN];
bool vis[MaxN];
pair<int, int> E[MaxN];
struct Edge { int v, nxt; } e[2 * MaxN];
set<pair<int, int> > Graph;
set<set<pair<int, int> > > S;
inline void AddEdge(int u, int v) {
	e[++tot].nxt = head[u], head[u] = tot;
	e[tot].v = v;
}
bool Check() {
	for (register int i = 1; i < n; i++) {
		if (!vis[i]) return false;
	}
	return true;
}
void DFS(int u) {
	if (Check()) {
		S.insert(Graph);
	}
	for (register int i = head[u]; i; i = e[i].nxt) {
		int v = e[i].v;
		if (vis[v]) continue;
		vis[v] = true;
		Graph.insert(make_pair(min(u, v), max(u, v)));
		DFS(v);
		vis[v] = false;
		Graph.erase(make_pair(min(u, v), max(u, v)));
	}
}
int main() {
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	c = IO::Read(), T = IO::Read();
	while (T--) {
		n = IO::Read(), m = IO::Read();
		tot = 0;
		S.clear();
		memset(head, 0, sizeof(head));
		for (register int i = 1; i < n; i++) {
			int u = IO::Read(), v = IO::Read();
			E[i] = make_pair(u, v);
		}
		for (register int i = 1; i < n; i++) {
			for (register int j = i + 1; j < n; j++) {
				if (E[i].first == E[j].first || E[i].first == E[j].second || E[i].second == E[j].first || E[i].second == E[j].second) {
					AddEdge(i, j);
					AddEdge(j, i);
				}
			}
		}
		for (register int i = 1; i <= m; i++) DFS(IO::Read());
		cout << S.size() << endl;
	}
	return 0;
}
/*
1 1
4 1
1 2
2 3
2 4
1
*//*
7 1
5 2
1 2
1 3
2 4
2 5
1 3
*/
