#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define int long long
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
int d[100001];
int c[100001];
map<int,int> mp;
void solve(){
	mp.clear();
	int n,m,v;
	cin >>n>>m>>v;
	for(int i=1;i<=m;i++){
		cin >>c[i]>>d[i];
		if(mp[c[i]]){
			if(mp[c[i]]!=d[i]){
				cout <<0<<endl;
				return ;
			}
		}else mp[c[i]]=d[i];
	}
	if(m==1){
		cout <<qpow((v*v)%mod,n-1)<<endl;
		return ;
	}
	sort(c+1,c+m+1);
	int tm=unique(c+1,c+m+1)-c-1;
	int ans=1;
	for(int i=1;i<=tm;i++){
		if(i==1){
			if(c[1]==1){
				continue;
			}
			ans*=qpow((v*v)%mod,c[i]-1);
			ans%=mod;
			continue;
		}
		ans*=(qpow((v*v%mod),c[i]-c[i-1])-(qpow(v,c[i]-c[i-1]-1)*(v-1)%mod)+mod)%mod;
		ans%=mod;
	}
	if(c[tm]!=n){
		ans*=qpow((v*v)%mod,n-c[tm]);
		ans%=mod;
	}
	cout <<ans<<endl;
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t;
	cin >>t;
	while(t--)solve();
}
