#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5;
const int mod=1e9+7;
int c,T,n,k;
int u[maxn],v[maxn],a[maxn];
inline int rd(){
	int x=0,f=1;char s=getchar();
	while(s<'0'||s>'9'){
		if(s=='-')f=-1;
		s=getchar();
	}
	while(s>='0'&&s<='9'){
		x=x*10+s-'0';
		s=getchar();
	}
	return x*f;
}
inline int qpow(int x,int cs){
	if(cs==1)return x;
	if(cs==0)return 1;
	if(cs%2==0){
		int sum=qpow(x,cs/2)%mod;
		return (sum*sum)%mod;
	}
	else{
		int sum=qpow(x,(cs-1)/2)%mod;
		return ((x*sum%mod)*sum%mod);
	}
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	c=rd(),T=rd();
	while(T--){
		n=rd(),k=rd();
		for(int i=1;i<n;i++){
			u[i]=rd(),v[i]=rd();
		}
		int maxx=-10;
		for(int i=1;i<=k;i++){
			a[i]=rd();maxx=max(a[i],maxx);
		}
		cout<<qpow(n,k)%mod<<endl;
	}
	//system("pause");	
	return 0;
}
