#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll _=1e5+5,p=1e9+7;
ll sc,w,T,n,m,v;
pair<ll,ll>a[_];
inline ll ksm(ll x,ll y){
	ll z=1;
	for(;y;y>>=1,x=x*x%p)
		if(y&1)z=z*x%p;
	return z;
}
int main(){
	freopen("assign3.in","r",stdin);
	freopen("assign3.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--){
		cin>>n>>m>>v;sc=w=1;
		for(ll i=1,x,y;i<=m;i++)cin>>x>>y,a[i]={x,y};
		sort(a+1,a+m+1);
		for(ll i=2,j;i<=m;i++)
			if(a[i].first==a[i-1].first&&a[i].second!=a[i-1].second)sc=0;
			else if(a[i-1].first<a[i].first)j=a[i].first-a[i-1].first,w=w*(ksm(v,2*j)+p-ksm(v,j-1)*(v-1)%p)%p;
		w=w*ksm(v,2*(a[1].first-1))%p*ksm(v,2*(n-a[m].first))%p;
		cout<<sc*w<<'\n';
	}
	return 0;
}
