#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define pb push_back
#define lowbit(x) x&-x
using namespace std;
const int N=2e5+10;
const int mod=1e9+7;
struct node{
	int c,d;
}q[N];
ll ans;
int n,m,v,T;
inline int read(){
	int s=0,f=0;
	char ch=getchar();
	while(ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
	while(ch<='9'&&ch>='0') s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return f?-s:s;
}
inline int qpow(int x,int y){
	int res=1;
	while(y){
		if(y&1) res=1ll*res*x%mod;
		x=1ll*x*x%mod;
		y>>=1;
	}
	return res;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	T=read();
	while(T--){
		n=read(),m=read(),v=read();
		bool flg=0;map<int,int> a;
		for(register int i=1;i<=m;++i){
			q[i]={read(),read()};
			if(a[q[i].c]&&a[q[i].c]!=q[i].d) flg=1;
			a[q[i].c]=q[i].d;
		}
		if(flg) puts("0");
		else{
			sort(q+1,q+m+1,[](node a,node b){return a.c<b.c;});
			ans=qpow(1ll*v*v%mod,q[1].c-1+n-q[m].c);
			for(register int i=2;i<=m;++i) if(q[i].c!=q[i-1].c) ans=ans*(qpow(1ll*v*v%mod,q[i].c-q[i-1].c)-1ll*(v-1)*qpow(v,q[i].c-q[i-1].c-1)%mod+mod)%mod;
			cout<<ans<<'\n';
		}
	}
	return 0;
}
