#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=1e5+5;
const LL mod=1e9+7;
pair<int,int>p[N];
LL qpow(LL a,LL b){
	LL c=1;
	while(b){
		if(b&1)c=c*a%mod;
		a=a*a%mod;b>>=1;
	}
	return c;
}
void solve(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=1,x,y;i<n;++i){
		scanf("%d%d",&x,&y);
		p[i]={x,y};
	}
	sort(p+1,p+n);
	int cnt=unique(p+1,p+n)-p;
	printf("%lld\n",qpow(cnt-1,cnt-3)-(k==1?1:0));
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	int c,T;
	scanf("%d%d",&c,&T);
	while(T--)solve();
	return 0;
}
