#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mod=1e9+7;
ll t,n,m,v,p,cnt;
map<int,int> a;
ll fpow(ll x,ll k)
{
	ll ans=1;
	while(k)
	{
		if(k&1)ans=ans*x%mod;
		x=x*x%mod;
		k>>=1;
	}
	return ans;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	while(t--)
	{
		a.clear();
		p=cnt=0;
		scanf("%lld%lld%lld",&n,&m,&v);
		for(int i=1;i<=m;i++)
		{
			int c,d;
			scanf("%d%d",&c,&d);
			if(a[c]&&a[c]!=d)p=1;
			a[c]=d;
			if(a[c-1])cnt++;
		}
		if(p){printf("0\n");continue;}
		if(m>1)printf("%lld\n",fpow(v*v%mod,n-1-cnt)*fpow(v*v%mod-1,cnt)%mod);
		else printf("%lld\n",fpow(v*v%mod,n-1));
	}
	return 0;
}
