#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,mod=1e9+7;
struct node{int c,d;}p[N],b[N];
int T,n,m,v,a[N],len;
map<int,int> mp;
bool cmp(node x,node y){return x.c<y.c;}
inline int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1) ans=1ll*ans*a%mod;
		b>>=1;a=1ll*a*a%mod;
	}return ans;
}
int work(){
	scanf("%d%d%d",&n,&m,&v);mp.clear();len=0;
	if(n==1) return 0;
	for(int i=1;i<=m;i++) scanf("%d%d",&b[i].c,&b[i].d);
	for(int i=1;i<=m;i++){
		if(mp[b[i].c]){
			if(mp[b[i].c]!=b[i].d) return 0;
			continue;
		}mp[b[i].c]=b[i].d;
		p[++len]=b[i];
	}sort(p+1,p+1+m,cmp);
	int ans=0;
	if(p[1].c==1) ans=1;
	else ans=qpow(1ll*v*v%mod,p[1].c-1);
	for(int i=2;i<=len;i++)
		if(p[i-1].c+1==p[i].c)
			ans=1ll*ans*((1ll*v*v-v+1)%mod)%mod;
		else{
			int sum=1ll*v*(v-1)%mod*qpow(1ll*v*v%mod,p[i].c-p[i-1].c-1);
			int cnt=qpow(v,p[i].c-p[i-1].c-1);
			sum=(sum+cnt)%mod;
			ans=1ll*ans*sum%mod;
		}
	if(p[len].c!=n){
		ans=1ll*ans*qpow(1ll*v*v%mod,n-p[len].c)%mod;
	}
	return ans;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--) printf("%d\n",work());
	return 0;
}
