#include<bits/stdc++.h>
#define N 100007
#define mod 1000000007
#define ll long long

using namespace std;

inline int read(){
	int ret=0;char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9') ret=(ret<<3)+(ret<<1)+c-'0',c=getchar();
	return ret;
}

ll qpow(ll k,int x){
	ll ret=1;
	for(;x;k=k*k%mod,x>>=1) if(x&1) ret=ret*k%mod;
	return ret;
}

int n,m;
ll v;
map<int,int> mp;
vector<int> pos;

void mian(){
	n=read(),m=read(),v=read(),mp.clear(),pos.clear();
	for(int i=1;i<=m;i++){
		int u=read(),x=read();
		if(mp.find(u)!=mp.end()&&mp[u]!=x) return puts("0"),void();
		if(mp.find(u)==mp.end()) pos.push_back(u);
		mp[u]=x;
	}
	sort(pos.begin(),pos.end());
	ll ans=1;
	for(int i=0;i<pos.size()-1;i++){
		(ans*=qpow(v*v%mod,pos[i+1]-pos[i])-qpow(v,pos[i+1]-pos[i]-1)*(v-1)%mod)%=mod;
	}
	(ans*=qpow(v*v%mod,pos[0]-1))%=mod;
	(ans*=qpow(v*v%mod,n-pos.back()))%=mod;
	printf("%lld\n",ans<0?ans+mod:ans);
}

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