#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
const int mod=1e9+7;
int T,n,m,k,v,b[maxn],c,d,f,s;
unordered_map<int,int>mp;
inline int power(int a,int b) {
	int s=1;
	while(b) {
		if(b&1)s=s*a%mod;
		a=a*a%mod;
		b/=2;
	}
	return s;
}
signed 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--) {
		memset(b,0,sizeof(b));
		f=1;s=1;k=0;
		cin>>n>>m>>v;
		mp.clear();
		for(int i=1;i<=m;i++) {
			cin>>c>>d;
			if(mp[c]!=0) {
				if(mp[c]!=d)f=0;
			}else {
				b[++k]=c;
			}
			mp[c]=d;
		}
		if(!f) {
			cout<<"0\n";continue ;
		}
		if(m<=1) {
			cout<<power(v*v%mod,n-1)<<"\n";
			continue ;
		}
		sort(b+1,b+k+1);
		s=s*power(v*v%mod,b[1]-1)%mod;
		s=s*power(v*v%mod,n-b[k])%mod;
		for(int i=1;i<k;i++) {
			int t=b[i+1]-b[i];
			int u=(power(v*v%mod,t)-power(v,t-1)*(v-1)%mod+mod)%mod;
			s=s*u%mod;
		}
		cout<<s<<"\n";
	}
	return 0;
}
