#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+7;
const int mod=1e9+7;

struct Edge{
	int u,v,nxt;
};

inline int read(){
	int r=1,a=0;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')r=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		a=a*10+c-'0';
		c=getchar();
	}
	return a*r;
}

int cnt,n,k;

Edge t[N];
int ht[N],p[N];

void addTree(int u,int v){
	t[++cnt].v =v;
	t[cnt].u =u; 
	t[cnt].nxt =ht[u];
	ht[u]=cnt;
}

int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	
	int c,t;
	cin>>c>>t;
	while(t--){
		cnt=0;
		bool A=true,B=true;
		memset(ht,0,sizeof(ht));
		cin>>n>>k;
		for(int i=1;i<n;i++){
			int u,v;
			u=read(),v=read();
			if(u!=1||v!=i+1)B=false;
			if(u!=i||v!=i+1)A=false;
		}
		
		for(int i=1;i<=k;i++){
			scanf("%d",&p[i]);
		}
		if(A){
			cout<<1<<endl;
			continue;
		}else if(B){
			ll ans=n-2;
			ans*=1ll*(1ll*k*(2*n-1-k))/2%mod;
			ans%=mod;
			printf("%lld\n",ans);
		}
	}
	
    return 0;
}
