#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mod=1e9+7;
int t,n,m,cnt;
unordered_map<int,int> vis;
ll v,ans;
pair<int,int> lim[100010];
bool flag;
int read()
{
	int r=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') w=ch=='-'?-1:w,ch=getchar();
	while(ch>='0'&&ch<='9') r=r*10+ch-'0',ch=getchar();
	return r*w;
}
ll qp(ll x,int k)
{
	ll s=1;
	while(k)
	{
		if(k%2)
		s=s*x%mod;
		x=x*x%mod;
		k/=2;
	}
	return s;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	t=read();
	while(t--)
	{
		n=read(),m=read(),v=read();
		flag=cnt=0;
		vis.clear();
		for(int i=1;i<=m;i++)
		{
			lim[i].first=read(),lim[i].second=read();
			if(vis[lim[i].first]&&vis[lim[i].first]!=lim[i].second)
			flag=1;
			vis[lim[i].first]=lim[i].second;
		}
		if(flag)
		{
			puts("0");
			continue;
		}
		sort(lim+1,lim+m+1);
		m=unique(lim+1,lim+m+1)-lim-1;
		ans=qp(v*v%mod,lim[1].first-1);
		for(int i=2;i<=m;i++)
		if(lim[i-1].first==lim[i].first-1)
		ans=ans*(1+(v-1)*v%mod)%mod;
		else
		ans=ans*(qp(v*v%mod,lim[i].first-lim[i-1].first)-qp(v,lim[i].first-lim[i-1].first-1)*(v-1)%mod+mod)%mod;
		ans=ans*qp(v*v%mod,n-lim[m].first)%mod;
//		cout<<n<<' '<<m<<' '<<cnt<<endl;
		printf("%lld\n",ans);
	}
} 
