#include<bits/stdc++.h>
#define ll long long
#define sco 100010
#define mod 1000000007
using namespace std;
struct node{
	ll dis,k;
}a[sco];
ll T,n,m,w,ans;
map<ll,ll>mp;
bool cmp(node a,node b){
	return a.dis<b.dis;
}
ll ksm(ll a,ll b){
	ll res=1;
	while(b){
		if(b&1)res=res*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return res;
}
ll js(ll x,ll y){
	return ksm(w,y-x-1)*(w-1)%mod;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&T);
	while(T--){
		scanf("%lld%lld%lld",&n,&m,&w);
		mp.clear();
		bool flag=0;
		for(int i=1;i<=m;++i){
			scanf("%lld%lld",&a[i].dis,&a[i].k);
			if(mp[a[i].dis]!=0){
				if(mp[a[i].dis]!=a[i].k){
					flag=1;
				}
				else --i,--m;
			}else mp[a[i].dis]=a[i].k;
		}
		if(flag){
			printf("0\n");
			continue;
		}
		ans=ksm(w,2*n-2)%mod;
		sort(a+1,a+1+m,cmp);
		if(m<=1){
			printf("%lld\n",ans);
			continue;
		}
		ll cnt=ksm(w,2*(a[1].dis-1))*js(a[1].dis,a[2].dis)%mod;
		for(int i=3;i<=m;++i){
			ll y=a[i].dis,x=a[i-1].dis;
			cnt=((cnt*ksm(w,2*(y-x))%mod+js(x,y)*ksm(w,2*x-2)%mod)%mod+mod-js(x,y)*cnt%mod)%mod;
		}
		printf("%lld\n",(ans+mod-cnt*ksm(w,2*(n-a[m].dis))%mod)%mod);
	}
	return 0;
}
