#include<bits/stdc++.h>
using namespace std;
long long M=1e9+7;
long long T,n,m,v,aa[1000001],a,b,x;
long long ans=1;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--){
		x=0;
		ans=1;
		memset(aa,0,sizeof(aa));
		scanf("%d%d%d",&n,&m,&v);
		for(int i=1;i<=m;i++){
			cin>>a>>b;
			if(aa[a]!=0&&aa[a]!=b)x=1;
			else aa[a]=b;
		}
		if(x==1){
			cout<<0<<endl;
		}
		else{
			for(int i=1;i<n;i++){
				if(aa[i]!=0&&aa[i+1]==0){
					ans*=v*v;
					ans%=M;
				}
				else if(aa[i]!=0&&aa[i+1]!=0){
					ans*=(v*(v-1))+1;
					ans%=M;
				}
				else if(aa[i]==0&&aa[i+1]==0){
					ans*=v*v;
					ans%=M;
				}
				else{
					ans*=v*v;
					ans%=M;
				}
			}
			cout<<ans<<endl;
		}
		
	}
}
/*
1
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2
*/
