#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
const int maxn=1e5+5,mod=1e9+7;

int n,m,v;
inline int fp(int a,int k) {
	int res=1;
	while(k) {
		if(k&1) res=1ll*res*a%mod;
		k>>=1;
		a=1ll*a*a%mod;
	}
	return res;
}
map<int,int> mp;
int sub[maxn];

int dp[maxn];
inline void work() {
	int cnt=0;
	mp.clear();
	cin>>n>>m>>v;
	bool fg=1;
	for(int i=1;i<=m;++i) {
		int u,v; cin>>u>>v;
		if(!mp.count(u)) mp[u]=v;
		else if(mp[u]!=v) fg=0;
	}
	if(!fg) {
		cout<<"0\n";
		return;
	}
	if(m==1) {
		cout<<fp(v,n*2-2)<<'\n';
		return;
	}
	for(auto u:mp) {
		sub[++cnt]=u.first;
	}
	dp[1]=fp(v,sub[1]*2-2);
	for(int i=2;i<=cnt;++i) {
		dp[i]=1ll*dp[i-1]*v%mod*(v-1)*fp(v,(sub[i]-sub[i-1])*2-2)%mod;
		if(sub[i-1]!=sub[i]-1) dp[i]=(dp[i]+1ll*dp[i-1]*fp(v,(sub[i]-sub[i-1])*2-4))%mod;
	}
	cout<<dp[cnt]<<'\n';
}
int main() {
	ios::sync_with_stdio(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t; cin>>t;
	while(t--) work();
	return  0;
}
