#include<bits/stdc++.h>
using namespace std;
#define ll long long

ll c,T;
ll n,k;
ll f[200000];
ll u[200000],v[200000];
struct node{
	ll u,v;
}a[200000];
ll find(ll x){
	if(f[x]!=x) return x=find(f[x]);
}

int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	cin>>c>>T;
	while(T--){
		ll e;
		ll ans=0;
		cin>>n>>k;
		for(int i=1;i<n;i++){
			cin>>a[i].u>>a[i].v;
		}
		for(int i=1;i<=k;i++){
			cin>>e;
		}
		for(int i=1;i<=n;i++){
			if(a[i].u==e){
				for(int j=1;j<=n;j++){
					if(j!=i&&a[j].u==a[i].v) ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
