#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll T,m,n,v,ans,Por=1e9+7,s,t,mk,po[1000001];
void read(ll &x){
	x=0;bool f=0;char c=getchar();
	while(c<48||c>57)f=c==45?1:0,c=getchar();
	while(c>47&&c<58)x=(x<<3)+(x<<1)+c-48,c=getchar();
	if(f)x=-x;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
read(T);while(T--){
	ans=1;mk=0;
	scanf("%lld%lld%lld",&n,&m,&v);
	for(int i=1;i<=n;i++){
		po[i]=0;
	}
	for(int i=1;i<=m;i++){
		scanf("%lld%lld",&s,&t);
		if(po[s]!=0&&po[s]!=t)mk=1;
		po[s]=t;
	}
	if(mk){
		printf("0\n");
	}else{
		for(int i=1;i<n;i++)
			if(po[i]&&po[i+1]){
				ans=(ans*v%Por*v-v+1+Por)%Por;
			}else{
				ans=(ans*v%Por*v)%Por;
			}
		printf("%lld\n",ans%Por);
	}
}return 0;
}
