#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxm=1e5+1e3;
long long mod=1e9+7;
long long read(){
	long long a=0,f=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch){
		if(ch=='-') f=-f;
		ch=getchar();
	}
	while('0'<=ch&&ch<='9'){
		a=(a<<1)+(a<<3)+(ch^48);
		ch=getchar();
	}
	return a*f;
}
struct rec{
	long long c,d;
}en[maxm];
bool cmp(rec x,rec y){
	return x.c<y.c;
}
long long ksm(long long a,long long k){
	long long b=1;
	while(k){
		if(k&1){
			b=(b*a)%mod;
		}
		k>>=1;
		a=(a*a)%mod;
	}
	return b%mod;
}
long long n,m,v;
long long ans=0;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
//	printf("%lld",ksm(2,7));
	int T;
	scanf("%d",&T);
	while(T--){
		n=read();m=read();v=read();
		ans=1;
		for(int i=1;i<=m;i++){
			en[i].c=read();en[i].d=read();
		}
		sort(en+1,en+m+1,cmp);
		en[m+1].c=-1;en[m+1].d=-1;
		ans=(ans*(ksm(v,2*(en[1].c-1))))%mod;
		ans=(ans*(ksm(v,2*(n-en[m].c))))%mod;
		for(int i=2;i<=m;i++){
			if(en[i].c==en[i-1].c){
				if(en[i].d!=en[i-1].d){
					ans=0;
					break;
				}
				continue;
			}
			long long l=en[i].c-en[i-1].c;
//			printf("l=%lld\n",l);
			ans=(ans*((((ksm(v,2*l)+ksm(v,l-1)))%mod-(ksm(v,l))+mod)%mod))%mod;
		}
		printf("%lld\n",ans);
	}
	return 0;
}
