#include<bits/stdc++.h>
#define p 1000000007
using namespace std;
typedef long long ll;
ll qp(ll x,ll y){
	ll a=1,b=x;
	while(y){
		if(y%2==1) a=a*b%p;
		b=b*b%p;
		y/=2;
	}
	return a;
}
ll ts,n,m,k,x,y;
ll ans=1;
pair<ll,ll> a[100010];
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&ts);
	while(ts--){
		n=m=k=x=y=0;
		ans=1;
		memset(a,0,sizeof(a));
		scanf("%lld%lld%lld",&n,&m,&k);
		for(ll i=1;i<=m;i++){
			scanf("%lld%lld",&x,&y);
			a[i]={x,y};
		}
		sort(a+1,a+m+1);
		bool ok=1;
		for(ll i=1;i<=m;i++){
			if(i==1){
				ll u=a[i].first;
				ans=ans*qp(k,2*(u-1))%p;
				continue;
			}
			ll u=a[i].first,v=a[i-1].first;
			ll w=u-v-1;
			if(u==v){
				if(a[i].second==a[i-1].second) continue;
				printf("0\n");
				ok=0;
				break;
			}
			ll r=qp(k,w);
			ans=ans*((r*r%p-r+p)%p*k%p*k%p+r*(k*k%p-k+p+1)%p)%p;
		}
		ans=ans*qp(k,2*(n-a[m].first))%p;
		if(ok) printf("%lld\n",ans);
	}
}
