#include<bits/stdc++.h>
using namespace std;
unsigned long long mod=1e9+7,n,t,m,v,x[100005],nn,y,las,s,dan,wk,has;
map<int,int>mp;
unsigned long long ksm(long long now,int k)
{
	if(now==1)
	return 1;
	if(k==0)
	return 1;
	if(k==1)
	return now;
	if(k%2==0)
	{
		now=ksm(now,k/2);
		now=(now*now)%mod;
	}
	else
	{
		long long h=now;
		now=ksm(now,(k-1)/2);
		now=(((now*now)%mod)*h)%mod;
	}
	return now;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	while(t--)
	{
		cin>>n>>m>>v;
		nn=n;
		has=n;
		--n;
		las=-1;
		mp.clear();
		s=0;
		wk=0;
		for(int i=1;i<=m;++i)
		{
			cin>>x[i]>>y;
			if(mp[x[i]]!=0&&mp[x[i]]!=y)
				wk=1;
			else
			{
				if(mp[x[i]]==0)
				--has;
				mp[x[i]]=y;
			}
		}
		sort(x+1,x+1+m);
		for(int i=1;i<m;++i)
		{
			if(x[i]!=x[i+1])
			{
				--n;
				++s;
			}
		}
		if(wk)
		dan=0;
		else
		dan=(ksm((v*v)%mod,n)*ksm((v*(v-1)+1)%mod,s))%mod;
		if(v==2&&dan!=0)
		{
			dan=1;
			for(int i=1;i<m;++i)
			{
				if(x[i]!=x[i+1])
				{
					long long h=x[i+1]-x[i];
					dan=(dan*((ksm(v*v,h)-ksm(v,h-1)+mod)%mod))%mod;
				}
			}
			nn=x[1]-1+nn-x[m];
			dan=(dan*ksm(v*v,nn))%mod;
		}
		if(v!=1||dan==0)
		cout<<dan<<"\n";
		else
		cout<<1<<"\n";
	}
	return 0;
}
