#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,l,r) for(int i=l;i<=r;++i)
const int N=1e6+10,mod=1e9+7;
int qpow(int a,int b){
	if(b<0) return 0;
	int res=1;
	while(b){
		if(b&1) res=res*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return res;
}
int n,m,v;
struct Q{
	int c,d;
} q[N];
bool cmp(Q x,Q y){
	return x.c<y.c;
}
int ctot=0;
int get(int i){
	return (qpow(v,2*i+2)-(v-1)%mod*qpow(v,i))%mod;
//	return ((2*qpow(v,2*i)-qpow(v,i+1))%mod+mod)%mod;
}
void solve(){
	cin>>n>>m>>v;
	rep(i,1,m) cin>>q[i].c>>q[i].d;
//	dp[0]=(v*v-v+1)%mod,p[1]=1;
//	rep(i,2,1000000) p[i]=p[i-1]*v%mod*v%mod;
//	rep(i,1,1000000) dp[i]=(v*dp[i-1]+p[i]*v%mod*(v-1)%mod)%mod;
//	++ctot;
//	if(ctot!=4){
//		return ;
//	}
	sort(q+1,q+m+1,cmp);
	int cnt=0,f=1;
//	rep(i,1,m) cout<<q[i].c<<" ";
//	cout<<"\n";
	rep(i,1,m){
		if(q[i].c!=q[cnt].c) q[++cnt]=q[i];
		else{
			if(q[cnt].d!=q[i].d){
				f=0;
				break;
			}
		} 
	}
	m=cnt;
//	rep(i,1,cnt) cout<<q[i].c<<" ";
//	cout<<"\n";
//	cout<<v<<" "<<n<<" ";
	if(!f){
		cout<<0<<"\n";
		return ;
	}
	int len=0,ans=qpow(v,2*(q[1].c-1))%mod;
	rep(i,2,m){
		ans=(ans*get(q[i].c-q[i-1].c-1))%mod;
	}
	ans=(ans*qpow(v,2*(n-q[m].c)))%mod;
//	rep(i,q[m].c+1,n) ans=(ans*v%mod*v)%mod;
	cout<<(ans%mod+mod)%mod<<"\n";
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--) solve();	
	return 0;
} 
/*
1
10 5 2
2 1
3 1
4 1
7 1
10 1 

3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2
*/
