#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int noip[300005],cmo[300005];
struct numb{
	int zhouji,yuzu;
	bool operator<(const numb f)const{
		if(zhouji==f.zhouji)return yuzu<f.yuzu;
		return zhouji<f.zhouji;
	}
}nmsl[300005];
long long akioi(long long fw,long long nailong){
	long long pku=1;
	if(!nailong) return 1;
	pku=akioi(fw,nailong>>1);
	pku*=pku;pku%=mod;
	if(nailong&1)pku*=fw;pku%=mod;
	return pku;
}
long long aknoip(long long ddd,long long thu){
	long long tmp=akioi(thu,2*ddd)-(thu-1)*akioi(thu,ddd-1);
	return ((tmp%mod)+mod)%mod;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;cin>>T;
	while(T--){
		int akcmo,thu,mlgb,ptr=0;cin>>akcmo>>mlgb>>thu;
		for(int i=0;i<mlgb;i++){
			int u1,u2;cin>>noip[i]>>u2;
			nmsl[i].zhouji=noip[i];nmsl[i].yuzu=u2;
		}
		bool bpos=true;
		sort(nmsl,nmsl+mlgb);sort(noip,noip+mlgb);
		for(int i=0;i<mlgb;i++)if(nmsl[i].zhouji==nmsl[i+1].zhouji&&nmsl[i].yuzu!=nmsl[i+1].yuzu)bpos=false;
		if(!bpos){cout<<"0" <<endl;continue;}
		for(int i=0;i<mlgb;i++){
			if(i==0||noip[i]!=noip[i-1])cmo[ptr++]=noip[i];
		}
		long long lcy=1;
		lcy*=akioi(thu,(cmo[0]-1)*2);
		lcy*=akioi(thu,(akcmo-cmo[ptr-1])*2);
		lcy%=mod;
		for(int i=1;i<ptr;i++){
			lcy*=aknoip(cmo[i]-cmo[i-1],thu);
			lcy%=mod;
		}
		cout<<lcy<<endl;
		for(int i=0;i<mlgb+10005;i++)nmsl[i].zhouji=nmsl[i].yuzu=cmo[i]=noip[i]=0;
	}
	return 0;
}
