#include<bits/stdc++.h>
#define PII pair<int,int>
#define fir first
#define sec second
using namespace std;
const int N=1e5+5,P=1e9+7;
int n,m,V;
PII a[N],b[N];
int qpow(int a,int b){
	int ans=1;
	for(;b;b>>=1,a=1ll*a*a%P){
		if(b&1)ans=1ll*ans*a%P;
	}
	return ans;
}
int main(){
//	freopen("assign2.in","r",stdin);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;scanf("%d",&T);
	while(T--){
		scanf("%d%d%d",&n,&m,&V);
		for(int i=1;i<=m;i++)scanf("%d%d",&b[i].fir,&b[i].sec);
		sort(b+1,b+1+m);
		int ans=qpow(V,2*(n-1));
		if(m==1)printf("%d\n",ans);
		else{
			bool bk=1;
			int cnt=0;
			for(int i=1;i<=m;i++){
				if(b[i].fir==b[i-1].fir){
					if(b[i].sec==b[i-1].sec)continue;
					bk=0;break;
				}
				else a[++cnt]=b[i];
			}
			if(!bk){
				puts("0");
				continue;
			}
			if(cnt==1){
				printf("%d\n",ans);
				continue;
			}
			int res=0;
//			for(int i=1;i<=cnt;i++)printf("%d %d\n",a[i].fir,a[i].sec);
			for(int i=2;i<=cnt;i++){
//				(res+=1ll*qpow(V,2*(a[1].fir-1))*qpow(V,a[i].fir-a[1].fir-1)%P*(V-1)%P*qpow(V,2*(n-a[i].fir))%P)%=P;
				(res+=1ll*qpow(V,2*(a[1].fir-1))*qpow(V,a[i].fir-a[1].fir+1-i)%P*(V-1)%P*qpow(V,2*(n-a[i].fir))%P)%=P;
//				printf("%d\n",res);
			} 
			printf("%d\n",(ans-res+P)%P);
		}
	}
	return 0;
}
/*
1
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2
*/
