#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
map<int,int> mp;
vector<int> a; 
int ksm(int a,int b){
	int ans=1;
	for(;b>=1;b>>=1){
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
	}
	return ans;
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int TestCase=read();
	while(TestCase--){
		a.clear(),mp.clear();
		int n=read(),m=read(),v=read();
		bool tg=0;
		for(int i=1;i<=m;i++){
			int x=read(),w=read();
			if(mp.find(x)!=mp.end()){
				if(mp[x]!=w) tg=1;
				continue;
			}
			a.push_back(x);
			mp[x]=w;
		}
		if(tg){
			printf("0\n");
			continue;
		}
		sort(a.begin(),a.end());
		int ans=1;
		for(int i=0;i<a.size();i++){
			int u=a[i];
			if(i==0){
				ans=ans*ksm(v,2*(u-1))%mod;
			}
			if(i+1==a.size()){
				ans=ans*ksm(v,2*(n-1-u+1))%mod;
				break;
			}
			ans=ans*((ksm(v,2*(a[i+1]-a[i]))-ksm(v,a[i+1]-a[i]))%mod+mod+ksm(v,a[i+1]-a[i]-1))%mod;
		}
		printf("%lld\n",ans);
	//	cout<<ksm(86*86-85,502);
	}
	//ǰ25pts(30min) 
}
