#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1,mod=1e9+7;
typedef long long ll;
typedef unsigned long long ull;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<3)+(x<<1)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
int a[N],t,n,m,ml,v,x,y,fl;
ull qmi(ull x,ull y){
	ull res=1;
	while(y){
		if(y&1)res=(res*x)%mod;
		x=x*x%mod;
		y>>=1;
	}
	return res%mod;
}
typedef pair<int,int>pii;
pii lim[N];
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	t=read(); 
	for(int j=1;j<=t;++j){
		n=read(),ml=m=read(),v=read(),fl=0;//A
		if(m==n)fl=1;
		for(int i=1;i<=m;++i)x=read(),y=read(),lim[i]={x,y},fl=(x==i)?fl:0;
		if(fl)cout<<qmi((v-1)*v+1,n-1)%mod<<endl;
		else{
			cout<<0<<'\n';
		}
	}
	return 0;
}
/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2
*/
/*

*/
