#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
ll T,n,m,v,c[100005],d[100005],tot,ans;
ll uc[100005],cnt;
map<int,int>mp;
bool flagA,flagZ;
inline ll qpow(ll a,ll b){
	ll ret=1;
	while(b){
		if(b&1)ret=ret*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ret%mod;
} 
inline void A(){
	ll sum=qpow(v*(v-1)%mod+1,n-1);
	cout<<sum<<"\n";
}
bool cmp(const ll &a,const ll &b){
	return a<b;
}
inline void D(){
	ll sum=qpow(v*v%mod,n-1);
	cout<<sum<<"\n";
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--){
		cnt=ans=tot=0;
		cin>>n>>m>>v;
		flagA=1;flagZ=0;
		mp.clear();
		for(int i=1;i<=m;i++){
			cin>>c[i]>>d[i];
			if(mp[c[i]]!=0&&mp[c[i]]!=d[i])flagZ=1;
			else mp[c[i]]=d[i];
			if(c[i]!=i)flagA=0;
		}
		if(flagZ){cout<<"0\n";continue;}
		if(n==m&&flagA){A();continue;}
		if(m==1){D();continue;}
		sort(c+1,c+m+1,cmp); 
		for(int i=2;i<=m;i++){
			if(c[i-1]==c[i]-1)tot++;
			if(c[i]!=c[i-1])uc[++cnt]=c[i];
		}
		ans=qpow(v*(v-1)%mod+1,tot);
		ans*=qpow(v*v%mod,n-tot-1);
		ans%=mod;
		for(int i=2;i<=cnt;i++){
			ll k=uc[i]-uc[i-1]-1; 
			if(k==0)continue;
			ll dc=(v-1)*qpow(v,k+1)%mod;
			ans-=dc;
		}
		while(ans<0)ans+=mod;
		ans%=mod;
		cout<<ans<<"\n";
	}
	return 0;
}

