#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define N (int)(1e6+10)
#define mod (ll)(1e9+7)

int T;
int n,m;
ll v;

int c[N],d[N];
int vis[N];

inline void solve(){
	scanf("%d %d %lld",&n,&m,&v);
	bool fl=1;
	for(int i=1;i<=m;++i){
		scanf("%d %d",&c[i],&d[i]);
		if(vis[c[i]]&&vis[c[i]]!=d[i]) fl=0;
		vis[c[i]]=d[i];
	}
	ll ans=1;
	for(int i=1;i<n;++i){
		if(!vis[i]){
			if(!ans) ans=(ans+v*v%mod)%mod;
			else ans=ans*v%mod*v%mod;
		}
		else if(vis[i]&&vis[i+1]){
			ans=(ans*(v*v%mod-(v-1)+mod)%mod)%mod;
		}
		else ans=ans*v%mod*v%mod;
	}
	for(int i=1;i<=n;++i) vis[i]=0;
	if(!fl){
//		printf("orz\n");
		ans=0;
	}
	printf("%lld\n",ans);
}

int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--) solve();
	return 0;
}
