#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=1e5+5,M=1e9+7;
ll n,m,v,a[N],ans,cnt;
unordered_map<int,int>smi1e;
ll ksm(ll x,ll p){
	ll ans=1;
	for(;p>0;x=x*x%M,p>>=1)
		if(p&1) ans=ans*x%M;
	return ans;
}
ll calc(ll dis){
	if(dis==-1) return 0;
	return ((ksm(v,2ll*dis+2)-ksm(v,dis+1)+M)%M+ksm(v,dis))%M;
}
void solve(){
	smi1e.clear();
	ans=1,cnt=0;
	bool flag=false;
	cin>>n>>m>>v;
	for(int i=1,c,d;i<=m;i++){
		cin>>c>>d;
		if(smi1e[c]==0) smi1e[c]=d,a[++cnt]=c;
		else if(smi1e[c]!=d) flag=true;
	}
	if(flag){
		cout<<"0\n";
		return;
	}
	sort(a+1,a+cnt+1);
	for(int i=2;i<=cnt;i++) ans=ans*calc(a[i]-a[i-1]-1)%M;
	if(a[1]!=1) ans=ans*ksm(v*v%M,a[1]-1)%M;
	if(a[cnt]!=n) ans=ans*ksm(v*v%M,n-a[cnt])%M;
	cout<<ans<<"\n";
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int T;
	cin>>T;
	while(T--) solve();
	return 0;
}
