#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
inline ll read(){
	ll x=0;
	int c=getchar();
	bool f=0;
	while(c<48||c>57) f=(c=='-'),c=getchar();
	while(c>47&&c<58) x=(x<<3)+(x<<1)+(c&15),c=getchar();
	return f?-x:x;
}
inline void write(ll x,char end='\n'){
	if(x==0){
		putchar('0');
		putchar(end);
		return;
	}
	if(x<0) putchar('-'),x=-x;
	short ch[30]={0},cnt=0;
	while(x) ch[cnt++]=x%10,x/=10;
	while(cnt--) putchar(ch[cnt]^48);
	putchar(end);
}
const ll mod=1e9+7;
inline ll fp(ll x,ll y){
	ll s=1;
	while(y){
		if(y&1) s=s*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return s;
}
const int N=1e5+5;
int n,m,v;
struct node{int c,d;}e[N];
inline bool cmp(node x,node y){
	return x.c<y.c;
}
inline void solve(){
	n=read(),m=read(),v=read();
	for(int i=1;i<=m;++i) e[i]={(int)read(),(int)read()};
	sort(e+1,e+m+1,cmp);
	for(int i=1;i<m;++i){
		if(e[i].c==e[i+1].c&&e[i].d!=e[i+1].d){
			puts("0");
			return;
		}
	}
	ll ans=fp(v,2*(e[1].c-1))*fp(v,2*(n-e[m].c))%mod;
	for(int i=1;i<m;++i){
		if(e[i].c==e[i+1].c) continue;
		int ti=e[i].c,tj=e[i+1].c;
		ans=ans*((fp(v,2*(tj-ti))-fp(v,tj-ti-1)*(v-1)%mod)%mod)%mod;
	}
	ans=(ans%mod+mod)%mod;
	write(ans);
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T=read();
	while(T--) solve();
	return 0;
}
