#include<cstdio>
#include<algorithm>
#define ll long long
char Begin;
int read(){
	int num=0;
	char ch;ch=getchar();
	while(ch<48||ch>57)ch=getchar();
	while(ch>47&&ch<58){
		num=(num<<1)+(num<<3)+(ch^48);
		ch=getchar();
	}return num;
}
const int N=1e5+5;
const ll mod=1e9+7;
ll Pow(ll x,ll k){
	ll sum=1;
	while(k){
		if(k&1)(sum*=x)%=mod;
		(x*=x)%=mod;
		k>>=1;
	}return sum;
}
struct node{
	int note;
	int num;
};
node a[N];
int whe[N];
int n,m,K,o;
ll ans,sum;
bool cmp1(node x,node y){return x.note<y.note;}
ll calc(int len){
	return (Pow(K,len)*(Pow(K,len)-1)%mod+Pow(K,len-1))%mod;
}
void work(){
	o=0,n=read(),m=read(),K=read();
	for(int i=1;i<=m;i++)a[i].note=read(),a[i].num=read();
	std::stable_sort(a+1,a+1+m,cmp1);
	for(int i=1;i<m;i++)if(a[i].note==a[i+1].note&&a[i].num!=a[i+1].num){printf("0\n");return;}
	for(int i=1;i<=m;i++)
		if(!o||whe[o]!=a[i].note)
			whe[++o]=a[i].note;
	ans=1;
	for(int i=2;i<=o;i++)
		(ans*=calc(whe[i]-whe[i-1]))%=mod;
	(ans*=Pow(1ll*K*K%mod,whe[1]-1+n-whe[o]))%=mod;
	printf("%lld\n",ans);
}
char End;
int main(){
//	printf("%d\n",(&End-&Begin)/1024);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int Case=read();
	while(Case--)work();
	return 0;
}
