#include<bits/stdc++.h>
#define FIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int N=1e5+5;
const int mod=1e9+7;

int qpw(int a,int b){
	int as=1;
	while(b){
		if(b&1) as=1ll*as*a%mod;
		a=1ll*a*a%mod,b>>=1;
	}
	return as;
}

int n,m,lim,as,cnt,P,Q;
pii a[N],vc[N];

void solve(){
	scanf("%d%d%d",&n,&m,&lim);
	P=1ll*lim*lim%mod;
	Q=(1ll*lim*(lim-1)%mod+1)%mod;
	
	for(int i=1;i<=m;++i) scanf("%d%d",&vc[i].fi,&vc[i].se);
	sort(vc+1,vc+m+1);
	for(int i=1;i<m;++i){
		if(vc[i].fi!=vc[i+1].fi) continue;
		if(vc[i].se!=vc[i+1].se){
			puts("0");return;
		}
	} 
	if(lim==1){puts("1");return;}
	cnt=0,a[++cnt]=vc[1]; 
	for(int i=2;i<=m;++i){
		if(vc[i].fi==vc[i-1].fi) continue;
		a[++cnt]=vc[i];
	}
	
	if(!cnt){
		as=qpw(P,n-1),printf("%d\n",as);
		return;
	}
	as=qpw(P,a[1].fi-1);
	
	for(int i=1,j=1;i<=cnt;++i){
		j=max(j,i);
		while(j<cnt && a[j+1].fi==a[i].fi+j-i+1) ++j;
		if(j==cnt){
			int x=a[i].fi,y=a[j].fi;
			int w=qpw(Q,y-x);
			as=1ll*as*w%mod;
			if(y<n) as=1ll*as*qpw(P,n-y)%mod;
			break;	
		}
		int x=a[i].fi,y=a[j].fi,z=a[j+1].fi;
		int w=qpw(Q,y-x);
		int u=1ll*qpw(lim,z-1-y)*Q%mod;
		int v=1ll*(qpw(P,z-1-y)-qpw(lim,z-1-y)+mod)%mod*P%mod;
		as=1ll*as*w%mod;
		as=(1ll*u+v)%mod*as%mod;
		i=j;
	}
	printf("%d\n",as);
}

signed main(){
	FIO("assign");
	int T; scanf("%d",&T);
	while(T--) solve();
	return 0;
}