#include<bits/stdc++.h>
using namespace std;
const int N=100005;
const int mod=1e9+7;
int _;
int n,m,V;
int qpow(int x,int p){
	int ret=1;
	while(p){
		if(p&1) ret=1ll*ret*x%mod;
		x=1ll*x*x%mod;
		p>>=1; 
	} return ret;
}
unordered_map<int,int> mp;
int c[N],d[N];
int ans;
void Solve(){
	scanf("%d%d%d",&n,&m,&V);
	bool flg=true;
	for(int i=1;i<=m;i++){
		scanf("%d%d",&c[i],&d[i]);
		if(mp[c[i]]&&mp[c[i]]!=d[i]) flg=false;
		mp[c[i]]=d[i];
	}
	sort(c+1,c+m+1);
	m=unique(c+1,c+m+1)-c-1;
	for(int i=1;i<=m;i++) mp[c[i]]=0;
	if(!flg) return puts("0"),void();
	ans=1;
	for(int i=1;i<m;i++){
		int len=c[i+1]-c[i];
		int ret1=1ll*(V-1)*qpow(V,len-1)%mod;
		int ret2=(qpow(V,len<<1)-ret1+mod)%mod;
		ans=1ll*ans*ret2%mod;
	}
	ans=1ll*ans*qpow(V,(n-c[m])<<1)%mod;
	ans=1ll*ans*qpow(V,(c[1]-1)<<1)%mod;
	printf("%d\n",ans);
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&_);
	while(_--) Solve();
	return 0;
}
/*
1
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2
*/
