#include<iostream>
#include<utility>
#include<algorithm>
#include<cstdio>
#define int long long
#define endl "\n"
using namespace std;
const int M=1e5+5,mod=1e9+7;
pair<int,int> p[M];
int power(int x,int y){
	int ans=1;
	while(y){
		if(y&1)ans=ans*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return ans;
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin.tie(0);cout.tie(0);
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--){
		int n,m,v;
		cin>>n>>m>>v;
		for(int i=1;i<=m;i++)cin>>p[i].first>>p[i].second;
		sort(p+1,p+1+m);
		bool f=true;
		for(int i=1;i<m;i++){
			if(p[i].first==p[i+1].first && p[i].second!=p[i+1].second)f=false;
		}
		if(!f){
			cout<<0<<endl;
			continue;
		}
		int ans=power(v,2*p[1].first-2)*power(v,2*n-2*p[m].first)%mod;
//		cout<<ans<<endl;
		for(int i=1;i<m;i++){
			int l=p[i].first,r=p[i+1].first;
			if(l==r)continue;
			ans=ans*(power(v,2*r-2*l)-power(v,r-l-1)*(v-1)%mod+mod)%mod;
		}
		cout<<ans<<endl;
	}
}
