#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1000000007;

int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f*=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x*=10;
		x+=ch-'0';
		ch=getchar();
	}
	return x*f;
} 

int T,n,m,v;
int x[100001000];

int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	T=read();
	while(T--){
		ll ans=1ll;
		bool flag=0;
		n=read();m=read();v=(ll)read();
		for(int i=1;i<=n;i++)x[i]=0;
		for(int i=1;i<=m;i++){
			int c=read(),d=read();
			if(x[c]!=0&&!flag&&x[c]!=d){
				cout<<0<<endl;
				flag=1;
			}
			x[c]=d;
		}
		if(n>=100000000){
			cout<<0<<endl;
			continue;
		}
		if(flag)continue;
		for(int i=1;i<n;i++){
			ll sum=0ll;
			if(x[i]&&x[i+1]){
				sum=(v*v-v+1)%mod;
			}else{
				sum=(v*v)%mod;
			}
			ans=ans*sum%mod;
		}
		cout<<ans<<endl;
	}
	return 0;
}
