#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=1e5+10,mod=1e9+7;
ll t,n,m,v;
map<ll,ll>mp;
struct node
{
	ll x,y;
	bool operator ==(const node &a)const
	{
		return x==a.x;
	}
}a[N];
bool cmp(node a,node b)
{
	return a.x<b.x;
}
ll qpow(ll x,ll y)
{
	ll ans=1;
	while(y)
	{
		if(y%2)ans*=x,ans%=mod;
		x*=x,x%=mod,y/=2;
	}
	return ans;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	while(t--)
	{
		mp.clear();
		ll chk=0;
		cin>>n>>m>>v;
		for(ll i=1;i<=m;i++)
		{
			ll &x=a[i].x,&y=a[i].y;
			scanf("%lld%lld",&x,&y);
			if(mp[x]&&mp[x]!=y)chk=1;
			else mp[x]=y;
		}
		if(chk)
		{
			puts("0");
			continue;
		}
		sort(a+1,a+1+m,cmp);
		m=unique(a+1,a+1+m)-a-1;
		ll ans=max(qpow(v,2*(a[1].x-1)),1ll)*max(qpow(v,2*(n-a[m].x))%mod,1ll)%mod;
		for(ll i=1;i<m;i++)
		{
			ll l=a[i].x,r=a[i+1].x;
			if(l+1==r)
				ans=ans*((v-1)*v%mod+1)%mod;
			else
			{
				ll k=r-l;
				ans*=(qpow(v,2*k)-qpow(v,k-1)*(v-1)%mod+mod)%mod;
				ans%=mod;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
/*
long long 
1
5 1 2
1 2
*/
