#include<bits/stdc++.h>
#define ll long long
#define D double
#define F(i,j,n) for(register int i=j;i<=n;i++)
#define lc id*2
#define rc id*2+1
using namespace std;
const int N=1e6+10;
ll n,m,k,y,u,v,w,cnt,ans,sum,t,T;
ll mn=1e9,mx=-1e9,p=1e9+7;
struct Node{
	ll c,d;
}a[N];
bool cmp(Node a,Node b){
	if(a.c==b.c) return a.d<b.d;
	return a.c<b.c;
}
ll f(ll a,ll b){
	ll ans=1;
	while(b){
		if(b%2) ans*=a,ans%=p;
		a*=a;
		a%=p;
		b/=2;
	}
	return ans;
}
void solve(){
	cin>>n>>m>>v;
	cnt=0;
	F(i,1,m) cin>>a[i].c>>a[i].d;
	sort(a+1,a+1+m,cmp);
	F(i,1,m-1) if(a[i].c==a[i+1].c&&a[i].d!=a[i+1].d){
		cout<<0<<"\n";
		return ;
	}
	F(i,1,m-1) if(a[i].c==a[i+1].c-1) cnt++;
	cout<<(f((v*(v-1)+1)%p,cnt)*f(v,2*(n-1-cnt))%p)%p<<"\n";
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	while(T--) solve();

	return 0;
}

