#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
inline long long read(){
	long long x=0,fh=1;
	char ch=getchar(),lst='%';
	while(ch<'0'||ch>'9')lst=ch,ch=getchar();
	if(lst=='-')fh=-1;
	while(ch>='0'&&ch<='9')x=x*10+ch-48,ch=getchar();
	return x*fh;
}
struct node{
	int c,d;
}q[100005];
int cmp(node a,node b){
	return a.c<b.c;
}
long long ksm(long long a,long long b){
	long long ans=1;
	while(b){
		if(b&1)ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
int n,m,T;
long long v,ans;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	T=read();
	while(T--){
		n=read(),m=read(),v=read();
		for(int i=1;i<=m;i++){
			q[i].c=read(),q[i].d=read();
		}
		sort(q+1,q+m+1,cmp);
		ans=ksm(v,(q[1].c-1)*2)*ksm(v,(n-q[m].c)*2)%mod;
		for(int i=2;i<=m;i++){
			if(q[i].c==q[i-1].c&&q[i].d!=q[i-1].d){
				ans=0;
				break;
			}
			if(q[i].c==q[i-1].c)continue;
			ans=(ans*((ksm(v,(q[i].c-q[i-1].c)*2)-ksm(v,q[i].c-q[i-1].c-1)*(v-1)%mod+mod)%mod))%mod;
		}
		printf("%lld\n",ans);
	}
	return 0;
}
