#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1e9+7;
int ksm(int x,int y){
	int z=1;
	while(y){
		if(y&1){
			z=z*x%mod;
		}
		x=x*x%mod;
		y>>=1;
	}
	return z;
}
void solve(){
	int n,m,v;
	cin>>n>>m>>v;
	map<int,int> a;
	bool flag=false;
	for(int i=0;i<m;i++){
		int x,y;
		cin>>x>>y;
		if(a.find(x)!=a.end()){
			if(a[x]!=y){
				flag=true;
			}
		}
		a[x]=y;
	}
	if(flag){
		cout<<"0\n";
		return;
	}
	vector<int> b;
	for(auto i : a){
		b.push_back(i.first);
	}
	int ans=ksm(v,(n-1-b.back()+b[0])*2);
	for(int i=1;i<(int)b.size();i++){
		int x=(ksm(v,(b[i]-b[i-1])*2)-(v-1)*ksm(v,b[i]-b[i-1]-1)%mod+mod)%mod;
		ans=ans*x%mod;
	}
	cout<<ans%mod<<'\n';
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false);
	int qwq=1;
	cin>>qwq;
	while(qwq--){
		solve();
	}
	return 0;
}
