#include<bits/stdc++.h>
#define x0 x_0
#define x1 x_1
#define y0 y_0
#define y1 y_1
#define yn y_n
#define j0 j_0
#define j1 j_1
#define d0 d_0
#define d1 d_1
#define k0 k_0
#define k1 k_1
#define LL long long
#define LD long double
using namespace std;
int t;
struct node{
	int c,d,id;
	bool operator < (const node &o)const{
		return (c!=o.c ? c<o.c : id<o.id);
	}
};
node a[100010];
LL n,m,v,p[100010];
const LL mod=1000000007;
LL qpow(LL a,LL b){
	a=(a%mod+mod)%mod;
	LL ret=1,pan=a;
	while(b){
		if(b&1) ret=(ret*pan)%mod;
		pan=(pan*pan)%mod,b>>=1;
	}
	return ret;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n>>m>>v;
		for(int i=1;i<=m;++i) cin>>a[i].c>>a[i].d,a[i].id=i,p[i]=a[i].c;
		if(m==1) {cout<<qpow(v,(n-1)<<1)<<"\n";continue;}
		sort(a+1,a+m+1);
		bool flag=0;
		for(int i=2;i<=m;++i)
			if(a[i].c==a[i-1].c && a[i].d!=a[i-1].d) {flag=1;break;}
		if(flag) {cout<<"0\n";continue;}
		sort(p+1,p+m+1);
		m=unique(p+1,p+m+1)-p-1;
		LL ans=1;
		LL x=p[1]-1;
		LL c=qpow(v,2*x);
		ans=(ans*c)%mod;
		for(int i=2;i<=m;++i){
			x=p[i]-p[i-1]-1;
			c=(qpow(v,x*2+2)-qpow(v,x+1)+qpow(v,x)+mod)%mod;
			ans=(ans*c)%mod;
		}
		if(p[m]^n){
			LL x=n-p[m];
			LL c=(qpow(v,2*x))%mod;
			ans=(ans*c)%mod;
		}
		cout<<ans<<"\n";
	}
	return 0;
}
/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2
*/
