#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N=100010,mo=1e9+7;
LL q,n,m,c,d,cnt,a[N];
map<LL,LL> f;
LL v,ans;
LL qp(LL a,LL t){
	LL s=1;
	while(t){
		if(t%2)s*=a;
		a*=a,t/=2;
		a%=mo,s%=mo;
	}
	return s;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&q);
	while(q--){
		scanf("%lld%lld%lld",&n,&m,&v);
		ans=1,cnt=0,f.clear();
		for(int i=1;i<=m;i++){
			scanf("%lld%lld",&c,&d);
			if(f.find(c)!=f.end()&&f[c]!=d){
				ans=0;
			}else if(f.find(c)==f.end()){
				a[++cnt]=c;
				f[c]=d;
			}
		}
		if(ans==0){
			printf("0\n");
			continue;
		}
		sort(a+1,a+cnt+1);
		ans=ans*qp(v,2ll*(a[1]-1))%mo;
		ans=ans*qp(v,2ll*(n-a[cnt]))%mo;
		for(int i=1;i<cnt;i++){
			ans=ans*(qp(v,2ll*(a[i+1]-a[i]))-(v-1)*qp(v,a[i+1]-a[i]-1)%mo)%mo;
			ans=(ans%mo+mo)%mo;
		}
		printf("%lld\n",ans);
	}
	return 0;
}
