#include<bits/stdc++.h>

#define int long long
#define N 1000001
#define MOD 1000000007
#define mp make_pair
#define pb push_back

using namespace std;

int qpow(int x,int y) {
	x%=MOD;
	int res=1;
	while(y) {
		if(y&1) {
			res=res*x%MOD;
		}
		x=x*x%MOD;
		y>>=1;
	}
	return res;
}

int n,m,v;
map<int,int> a;
void work() {
	cin>>n>>m>>v;
	a.clear();
	bool f=1;
	vector<int> pos;
	for(int i=1;i<=m;i++) {
		int c,d;
		cin>>c>>d;
		if(a[c]==0) {
			pos.pb(c);
		}
		if(a[c]!=0&&a[c]!=d) {
			f=0;
		}
		a[c]=d;
	}
	if(!f) {
		cout<<0<<endl;
		return ;
	}

	sort(pos.begin(),pos.end());	
	int ans=1;
	for(int i=0;i<(int)pos.size()-1;i++) {
		ans=ans*(qpow(v,2*(pos[i+1]-pos[i]))-qpow(v,pos[i+1]-pos[i]-1)*(v-1)%MOD)%MOD;
	}
	ans=ans*qpow(v,2*(pos.front()-1+n-pos.back()))%MOD;

	cout<<(ans%MOD+MOD)%MOD<<endl;
}

signed main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	
	int __;
	cin>>__;
	while(__--) {
		work();
	}

	return 0;
}