#include<bits/stdc++.h>
#define pb push_back
#define P make_pair
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define fi first
#define se second
using namespace std;
typedef long long ll;
inline ll read(){
	ll x=0;char c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
	return x;
}
typedef pair<int,int> pii;
const int N=1e6+3;
const int mod=1e9+7;
int n,m,V;
pii a[N];
inline int fp(int x,int p){
	int res=1;
	for(;p;p>>=1){
		if(p&1)res=1ll*res*x%mod;
		x=1ll*x*x%mod;
	}
	return res;
}
inline void Mi(int &x,int y){x=x<y?x-y+mod:x-y;}
inline void solve(){
	n=read(),m=read(),V=read();
	rep(i,1,m)a[i].fi=read(),a[i].se=read();
	sort(a+1,a+m+1);
	rep(i,1,m-1)if(a[i].fi==a[i+1].fi&&a[i].se!=a[i+1].se){
		printf("0\n");
		return ;
	}
	int lst=0;
	int ans=1;
	rep(i,1,m)if(a[i].fi!=lst){
		int x=a[i].fi;
		if(lst==0){
			ans=1ll*ans*fp(V,2*x-2)%mod;
		}
		else{
			int k=x-lst-1,res=fp(V,2*(k+1));
			Mi(res,1ll*(V-1)*fp(V,k)%mod);
			ans=1ll*ans*res%mod;
		}
		lst=x;
	}
	if(lst!=n)ans=1ll*ans*fp(V,2*(n-lst))%mod;
	printf("%d\n",ans);
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T=read();while(T--)solve();
	return 0;
}
