#include<bits/stdc++.h>

using namespace std;

#define ll long long

const int N=1e6+10,mod=1e9+7;

int T;
int n,m,c[N],d[N];
ll v,ans;
map<int,int> vis;
unordered_map<int,ll> dp[2];

ll qpow(ll a,ll b){
	ll res=1;
	while(b){
		if(b&1) res=res*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return res;
}

void sol(){
	scanf("%d %d %lld",&n,&m,&v);
	vis.clear();
	for(int i=1;i<=m;i++){
		scanf("%d %d",&c[i],&d[i]);
		if(vis.count(c[i])&&vis[c[i]]!=d[i]) return printf("0\n"),void();
		vis[c[i]]=d[i];
	}
	int lst=1;
	if(vis[1]) dp[0][1]=v-1,dp[1][1]=1;
	else dp[0][1]=v,dp[1][1]=0;
	for(auto _:vis){
		int i=_.first;
		if(i==1||i==n) continue;
		if(lst<i-1){
			dp[0][lst]=qpow(v,2ll*(i-lst-1))*(dp[0][lst]+dp[1][lst])%mod;
			dp[1][lst]=0;
		}
		dp[0][i]=(v*(v-1)%mod*dp[0][lst]%mod+(v-1)*dp[1][lst]%mod)%mod;
		dp[1][i]=(v*dp[0][lst]%mod+dp[1][lst])%mod;
		lst=i;
	}
	if(lst<n-1){
		dp[0][lst]=qpow(v,2ll*(n-lst-1))*(dp[0][lst]+dp[1][lst])%mod;
		dp[1][lst]=0;
	}
	if(vis[n]) ans=(v*dp[0][lst]%mod+dp[1][lst])%mod;
	else ans=(v*dp[0][lst]%mod+v*dp[1][lst]%mod)%mod;
	printf("%lld\n",ans);
}

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