#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10,mod=1e9+7;
typedef long long ll;
map<ll,ll> mp;
ll T,n,m,v;
ll ksm(ll x,ll y){
	ll ret=1;
	while(y){
		if(y&1) ret=ret*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return ret;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--){
		mp.clear();
		cin>>n>>m>>v;
		ll res=1,F=1,cnt=0;
		for(int i=1;i<=m;i++){
			int x,y;cin>>x>>y;
			if(mp[x]==y) continue;
			if(mp[x]!=0 && mp[x]!=y) {F=0;break;}
			mp[x]=y;
			if(x+1<=v && mp[x+1]) cnt++;
			if(x-1>=1 && mp[x-1]) cnt++;
		}
		if(!F) {cout<<"0\n";continue;}
		ll tem1=ksm(v*v%mod,n-cnt-1)%mod;
		ll tem2=ksm(((v-1)*v%mod+1)%mod,cnt);
		res=tem1*tem2%mod;
		cout<<res%mod<<"\n";
	}
	return 0;
}
