#include <iostream>
#include <set>
#include <map>
#include <unordered_map>
#include <random>
#include <cstring>
#include <ctime>
using namespace std;

const int maxn=1e5+5; 
const int seed=233; 
const int mod=1e9+7; 
int c, T;
int n, k;
vector<int> mp[maxn]; 
int tot; 
vector<int> nmp[maxn]; 
map<pair<int, int>, int> idx; 
map<pair<int, int>, int> hsh; 
set<long long> s; 
int ans=0; 
int vis[maxn]; 

void dfs(int x, int cnt, long long sum){
	if (cnt == n-1){
		s.insert(sum); 
		ans++; 
	}
	for (auto it: nmp[x]){
		if (vis[it]) continue;
		vis[it] = 1;
		pair<int, int> tmp = {min(x, it), max(x, it)}; 
		dfs(it, cnt+1, sum+hsh[tmp]); 
		vis[it] = 0; 
	}
}

int main(){
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout); 
	
	mt19937 rnd(time(0)); 
	
	cin >> c >> T;
	while (T--){
	
	s.clear(); 
	ans=0; 
	tot=0; 
	idx.clear();
	hsh.clear(); 
	
	cin >> n >> k; 
	for (int i=1; i<=n-1; i++){
		int u, v;
		cin >> u >> v;
		mp[u].push_back(v); 
		mp[v].push_back(u); 
		if (u>v) swap(u, v); 
		idx[{u, v}] = ++tot; 
	}
	for (int i=1; i<=n; i++){
		for (auto it: mp[i]){
			int u=i, v=it;
			if (u>v) swap(u, v); 
			for (auto itit: mp[i]){
				int uu=i, vv=itit;
				if (uu>vv) swap(uu, vv); 
				if (itit == it) continue; 
				nmp[idx[{u, v}]].push_back(idx[{uu, vv}]); 
				//cout <<  idx[{u, v}] << "->" << idx[{uu, vv}] << endl; 
				if (idx[{u, v}] > idx[{uu, vv}]) continue; 
				hsh[{idx[{u, v}], idx[{uu, vv}]}] = rand(); 
			}
		}
	}
	for (int i=1; i<=k; i++){
		int a;
		cin >> a;
		vis[a] = 1; 
		dfs(a, 1, 0); 
		vis[a] = 0;
	}
	cout << s.size() << endl; 
	for (int i=1; i<=tot; i++) nmp[i].clear(); 
	for (int i=1; i<=n; i++) mp[i].clear(); 
	}
	
	return 0; 
}

