#include<bits/stdc++.h>
using namespace std;
int t,n,m,v;
typedef long long ll;
const ll mod=1e9+7;
vector<pair<int,int> > p;
ll qpow(ll a,ll b) {
	ll res=1,res2=a;
	while(b) {
		if(b&1) {
			res=res*res2%mod;
		}
		res2=res2*res2%mod;
		b>>=1;
	}
	return res;
}
int main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	while(t--) {
		cin>>n>>m>>v;
		ll ans=1,f[100005],tmp=1,tmp2=1;
		int realm=m;
		p.clear();
		for(int i=1;i<=100000;i=-~i) {
			f[i]=tmp;
			tmp2=(tmp2*v%mod)*(~-v)%mod;
			tmp=(tmp+tmp2)%mod;
		}
		for(int i=0;i<m;i=-~i) {
			int u,v;
			cin>>u>>v;
			p.push_back(make_pair(u,v));
		}
		sort(p.begin(),p.end());
		int cnt=1;
		for(int i=1;i<m;i=-~i) {
			if(p[i].first==p[~-i].first) {
				if(p[i].second!=p[~-i].second) {
					ans=0;
					break;
				}
				else {
					realm=~-realm;
				}
			}
			if(p[i].first==-~p[~-i].first) {
				cnt++;
			}
			else {
				ans=(ans*f[cnt])%mod;
				cnt=1;
			}
		}
		ans=(ans*f[cnt])%mod;
		if(n>realm) ans=(ans*qpow(qpow(v,(n-realm)),2))%mod;
		cout<<ans<<'\n';
	}
	return 0;
}
