#include <bits/stdc++.h>
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1;
	for(;isdigit(ch);ch=getchar()) x=(x<<3)+(x<<1)+(ch&15);
	return f*x;
}
const int mod=1000000007;
int t,n,m,v;
pair<int,int> p[100005];
int Pow(int x,int y){
	int res=1;
	for(;y;y>>=1,x=1ll*x*x%mod)
		if(y&1)
			res=1ll*res*x%mod;
	return res;
}
int solve(){
	for(int i=1;i<m;i++)
		if(p[i].first==p[i+1].first&&p[i].second!=p[i+1].second)
			return 0;
	int res=1ll*Pow(v,2*p[1].first-2)*Pow(v,2*n-2*p[m].first)%mod;
	for(int i=1;i<m;i++)
		if(p[i+1].first!=p[i].first)
			res=1ll*res*(Pow(v,2*(p[i+1].first-p[i].first))-1ll*Pow(v,p[i+1].first-p[i].first-1)*(v-1)%mod)%mod;
	return (res+mod)%mod;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	t=read();
	while(t--){
		n=read(),m=read(),v=read();
		for(int i=1;i<=m;i++) p[i].first=read(),p[i].second=read();
		sort(p+1,p+m+1);
		printf("%d\n",solve());
	}
	return 0;
}
/*
1
2 3 2
1 1
1 1
1 1
*/
