#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll p=1e9+7,M=1e5+10;
ll T,n,m,v;
struct node{
	ll c,d;
}a[M];
bool cmp(node x,node y){
	return x.c<y.c;
}
ll ksm(ll x,ll y){
	ll res=1;
	while(y){
		if(y&1ll) res=(res*x)%p;
		y=y/2;
		x=x*x%p;
	}
	return res;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	while(T--){
		cin>>n>>m>>v;
		ll ans=1;
		for(ll i=1,d;i<=m;i++) cin>>a[i].c>>a[i].d;
		sort(a+1,a+m+1,cmp);
		bool flag=0;
		ans=ksm(v,2*(a[1].c-1))*ksm(v,2*(n-a[m].c))%p;
		for(ll i=2;i<=m;i++){
			if(a[i].c==a[i-1].c){
				if(a[i].d!=a[i-1].d){
					flag=1;
					break;
				}
			}
			else{
				ll x=a[i].c-a[i-1].c-1;
//				if(x==0) ans=(v*(v-1)+1)%p*ans%p;
//				else
				ans=(ksm(v,x)*((v-1)*v+1)%p+ksm(v,2*x+1)*(v-1)%p*x%p+p)%p*ans%p;
			}
		}
		if(flag) cout<<0<<'\n';
		else cout<<(ans+p)%p<<'\n';
	}
	return 0;
}
