#include<bits/stdc++.h>
using namespace std;
struct node{
	vector<int>next;
}mp[100005];
long long n,k,ans;
int a[100005],b[100005];
int gj;
int jy[100005][4];
void dfs(int now,int last){
	for(int i=1;i<=jy[now][0];i++)
		if(jy[now][i]==last)return;
	jy[now][++jy[now][0]]=last;
	for(int i=0;i<mp[now].next.size();i++)
		if(mp[now].next[i]!=last)
			dfs(mp[now].next[i],now);
}
int sol(){
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(jy,0,sizeof(jy));
	ans=1;
	cin>>n>>k;
	for(int i=1;i<n;i++){
		cin>>a[i]>>b[i];
		mp[a[i]].next.push_back(b[i]);
		mp[b[i]].next.push_back(a[i]);
	}
	for(int i=1;i<=k;i++){
		cin>>gj;
		dfs(a[gj],a[gj]);
		dfs(b[gj],b[gj]);
	}
	for(int i=1;i<=n;i++){
		if(mp[i].next.size()==3){
			if(jy[i][0]==1)ans*=2;
			else ans*=3;
			ans%=1000000007;
		}
	}
	cout<<ans<<endl;
	for(int i=1;i<=n;i++)mp[i].next.clear();
}
int main(){
	freopen("traverse5.in","r",stdin);freopen("traverse.out","w",stdout);
	int c,t=1;
	cin>>c>>t;
	for(int i=1;i<=t;i++){
		sol();
	}
}
/*
10 5 2
10 2
7 2
2 2
4 2
3 2

129600

4 4 4
3 3
 3 3 5 5

3 3
4 4 4
 4 4 4 4


*/
