#include<bits/stdc++.h>
using namespace std;
const long long MAXN=1e5+5,MOD=1e9+7;
long long t;
long long n,m,v;
int a[MAXN];
int cnt;
map<long long,long long> mp;
long long ksm(long long x,long long num){
	long long tmp=x,ans=1ll;
	while(num){
		if(num&1ll){
			ans*=tmp;
			ans%=MOD;
		}
		tmp*=tmp;
		tmp%=MOD;
		num/=2;
	}
	return ans;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&t);
	while(t--){
		cnt=0;
		mp.clear();
		scanf("%lld%lld%lld",&n,&m,&v);
		bool flag=false;
		for(int i=1;i<=m;i++){
			long long x,y;
			scanf("%lld%lld",&x,&y);
			if(mp[x]){
				if(mp[x]!=y){
					printf("0\n");
					flag=true;
					break;
				}
			}
			else{
				mp[x]=y;
				a[++cnt]=x;
			}
		}
		if(flag){
			continue;
		}
		sort(a+1,a+1+cnt);
		long long ans=1ll;
		for(int i=2;i<=cnt;i++){
			ans*=(ksm(v,2ll*(a[i]-a[i-1]))-ksm(v,a[i]-a[i-1]-1ll)*(v-1)%MOD+MOD)%MOD;
			ans%=MOD;
		}
		ans*=ksm(v,2ll*(n-a[cnt]));
		ans%=MOD;
		ans*=ksm(v,2ll*(a[1]-1));
		ans%=MOD;
		if(m==1){
			printf("%lld\n",ksm(v,2*n-2));
		}
		else{
			printf("%lld\n",ans);
		}
	}
}
