#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const int M=1e5+5;
int a[M];
LL qpow(LL a,LL b){
	LL c=1;a%=mod;
	while(b){
		if(b&1)c=c*a%mod;
		a=a*a%mod;b>>=1;
	}
	return c;
}
void solve(){
	int n,m;LL v;
	scanf("%d%d%lld",&n,&m,&v);
	map<int,int>mp;
	bool bj=0;
	int cnt=0;
	for(int i=1,x,y;i<=m;++i){
		scanf("%d%d",&x,&y);
		if(mp.find(x)!=mp.end()){
			if(mp[x]!=y)
				bj=1;
			else continue;
		}
		else{
			mp[x]=y;
			a[++cnt]=x;
		}
	}
	sort(a+1,a+1+cnt);
	if(bj){
		puts("0");
		return;
	}
	LL ans=1,res=1;
	if(a[1]>1){
		res=res*qpow(v,a[1]-1)%mod;
		ans=ans*qpow(v*v,a[1]-1)%mod;
	}
	for(int i=1;i<cnt;++i){
		if(a[i+1]-a[i]==1){
			ans=ans*(v*(v-1)%mod+1)%mod;
		}
		else{
			res=res*qpow(v,a[i+1]-a[i])%mod;
			ans=ans*qpow(v*v,a[i+1]-a[i])%mod;
		}
	}
	if(n>a[cnt]){
		res=res*qpow(v,n-a[cnt])%mod;
		ans=ans*qpow(v*v,n-a[cnt])%mod;//*(v*v-1)%mod;
	}
	printf("%lld\n",ans);
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	scanf("%d",&T);
	while(T--)solve();
	return 0;
}
