#include<bits/stdc++.h>

using namespace std;

const int N=1e5+5;
const long long mod=1e9+7;

long long t,n,m,v,c[N],d[N],ans=1;
map<int,int> p;

void clean(){
	ans=1;
	p.clear();
}

long long mi(long long o,long long p){
	long long res=1;
	while(p){
		if(p&1) res*=o,res%=mod;
		o*=o;
		o%=mod;
		p>>=1;
	}
	return res;
}

void solve(){
	cin>>n>>m>>v;
	for(int i=1;i<=m;i++){
		cin>>c[i]>>d[i];
		if(p[c[i]]&&p[c[i]]!=d[i]) ans=0;
		p[c[i]]=d[i];
	}
	if(!ans){
		cout<<ans<<"\n";
		return;
	}
	sort(c+1,c+m+1);
	int la=c[1],i=1;
	ans=mi(v,2*(c[1]-1));
	for(int i=1;i<=m;){
		while(c[i]==la&&i<=m) i++;
		if(i>m) break;
		ans*=mod+mi(v,2*(c[i]-la))-mi(v,c[i]-la-1)*(v-1)%mod;
		ans%=mod;
		la=c[i];
//		cout<<i<<" ";
	}
//	cout<<"\n";
	ans*=mi(v,2*(n-c[m])),ans%=mod;
	cout<<ans<<"\n";
}

int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	while(t--) clean(),solve();
}
