#include<bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define mod 1000000007
using namespace std;

ll T,n,m,v;
ll a[maxn],key[maxn];
ll c,d,flag;
ll cnt,ans;

ll binpow(ll x, ll pow){
	if (pow==1) return x;
	ll ret=1;
	ret=binpow(x,pow/2)*binpow(x,pow/2)%mod;
	if (pow%2==1) ret=ret*x%mod;
	return ret;
}

void init(ll x){
	for (ll i=1; i<=x; i++) {
		a[i]=0;
		key[i]=0;
	}
	return ;
}

int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	while(T--){
		cnt=flag=0;
		cin>>n>>m>>v;
		init(max(n,v));
		if (m==1){
			ans=binpow(v,2);
			ans=binpow(ans,n-1);
			cout<<ans<<endl;
			flag=1;
		}
		for (ll i=1; i<=m; i++) {
			cin>>c>>d;
			if (a[c]!=d && a[c]!=0){
				cout<<"0"<<endl;
				flag=1;
				break;
			}
			a[c]=d;
			if (a[c-1]!=0) {
				if (key[a[c-1]]!=0 && key[a[c-1]]!=d){
					cout<<"0"<<endl;
					flag=1;
					break;
				}
				
				if (key[a[c-1]]==0) cnt++;
				key[a[c-1]]=d;
			}
			if (a[c+1]!=0) {
				if (key[d]!=0 && key[d]!=a[c+1]){
					cout<<"0"<<endl;
					flag=1;
					break;
				}
				
				if (key[d]==0) cnt++;
				key[d]=a[c+1];
			}
		}
		if(flag==1) continue;
		ans=(v-cnt)*v%mod;
		ans=binpow(ans,n-1);
		ans=(ans+cnt)%mod;
		cout<<ans<<endl;
	}
	return 0;
}

