#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
#include<cmath>
using namespace std;
const long long N=1e5+7;
const long long INF=0x3f3f3f3f;
const long long mod=1e9+7;
map<long long,long long>kk;
map<long long,long long>pp;
long long t,n,m,v;
long long fpow(long long n,long long b){
	long long a=n,sum=1;
	while(b!=0){
		if(b%2==1){
			sum=(sum*a)%mod;
		}
		b>>=1;
		a=(a*a)%mod;
	}
	return sum;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld%lld",&n,&m,&v);
		long long mmv=0,ans=1;
		long long flag=1;
		for(long long i=1;i<=m;i++){
			long long a,b;
			scanf("%lld%lld",&a,&b);
			if(kk[a]!=0&&kk[a]!=b){
				flag=0;
			}else{
				kk[a]=b;
				if(kk[a-1]!=0&&pp[a-1]==0){
					pp[a-1]=1;
					mmv++;
				}
				if(kk[a+1]!=0&&pp[a]==0){
					pp[a]=1;
					mmv++;
				}
			}
		}
		kk.clear();
		pp.clear();
		if(flag==0){
			printf("0\n");
		}else{
			long long v1=(((v-1)*v)+1)%mod,v2=(v*v)%mod;
		//	cout << "the first:" << v1 << " " << mmv << endl << "the second" << v2 << " " << n-mmv-1 << endl;
			ans=(((ans*fpow(v1,mmv))%mod)*fpow(v2,n-mmv-1))%mod;
			printf("%lld\n",ans);
		}
			
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}

