#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N=1e5+10,P=1e9+7;
LL T,n,m,v,c[N],d[N];
map<LL,LL> vis;
LL ksm(LL a,LL b) { LL res=1; for(;b;b>>=1,a=a*a%P) if(b&1) res=res*a%P; return res; }
void solve()
{
	vis.clear();
	scanf("%lld%lld%lld",&n,&m,&v);
	for(int i=1;i<=m;i++)
	{
		scanf("%lld%lld",c+i,d+i);
		if(vis[c[i]]!=0&&vis[c[i]]!=d[i])
			return printf("0\n"),void();
		vis[c[i]]=d[i];
	}
	sort(c+1,c+m+1);
	m=unique(c+1,c+m+1)-c-1;
	LL ans=ksm(v,2*(c[1]-1));
	for(int i=2;i<=m;i++)
		ans=(ksm(v,2*(c[i]-c[i-1]))-ksm(v,c[i]-c[i-1]-1)*(v-1)%P+P)%P*ans%P;
	ans=ans*ksm(v,2*(n-c[m]))%P;
	printf("%lld\n",ans);
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&T);
	while(T--) solve();
	return 0;
}
