#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read() {
	int x=0,f=0; char c=getchar();
	for(;!isdigit(c);c=getchar()) f|=c=='-';
	for(;isdigit(c);c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	return f?-x:x;
}
const int N=2e6+10;
const ll mod=1e9+7;
int m,c[N],sum;
ll n,ans,val[N],v;
bool flag;
ll quick_pow(ll a,ll b) {
	if(b<0) return 1;
	ll res=1;
	while(b) {
		if(b&1) res=res*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return res;
}
void solve() {
	map<int,int> mp;
	flag=0; sum=0;
	n=read(),m=read(),v=read();
	for(int i=1,e,f,d;i<=m;++i) {
		e=f=read(),d=read();
		if(mp[f]) {
			if(mp[f]!=d) flag=1;
		} else {
			c[++sum]=e;
			mp[f]=d;
		}
	}
	sort(c+1,c+1+sum);
	if(flag==1||n==1) {puts("0");return ;}
	if(sum==1) ans=quick_pow(v,2*(n-1));
	else {
		ans=1;
		val[1]=(v-1)*v%mod+1;
		for(int i=2;i<sum;++i) 
			val[i]=val[i-1]*(v-1)*v+1;
		int x=0,cnt=0;
		for(int i=2;i<=sum;++i) {
			if(c[i-1]+1==c[i]) x++;
			else {
				ans=ans*val[x]%mod;
				cnt+=x;
				x=0;
			}
		}
		if(x) {
			ans=ans*val[x]%mod;
			cnt+=x;
		}
		ans=ans*quick_pow(v,2*(n-cnt-1)%(mod-1))%mod;
	}
	printf("%lld\n",ans);
}
int main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t=read();
	while(t--) solve();
	return 0;
}
/*
1
12 2 2
7 2
7 2
*/
