#include<bits/stdc++.h>
#define fo(i,l,r) for(int i=(l);i<=(r);++i)
#define fd(i,l,r) for(int i=(l);i>=(r);--i)
#define fu(i,l,r) for(int i=(l);i<(r);++i)
#define ll long long
using namespace std;
const int N=100007,mo=1e9+7;
int n,m,c,cnt;
struct node{
	int x,y;
}p[N],q[N];
ll ksm(ll x,int y){ll s=1;for(;y;y>>=1,x=x*x%mo)if(y&1)s=s*x%mo;return s;}
bool cmp(node u,node v){return u.x==v.x?u.y<v.y:u.x<v.x;}
ll calc(int x)
{
	return (ksm(c,x+x)+(mo-ksm(c,x-1))*(c-1))%mo;
}
void work()
{
	cnt=0;
	scanf("%d%d%d",&n,&m,&c);
	fo(i,1,m) scanf("%d%d",&p[i].x,&p[i].y);
	sort(p+1,p+m+1,cmp);
	fo(i,1,m)
	{
		int j=i;
		while(j<m&&p[j+1].x==p[i].x) j++;
		if(p[j].y!=p[i].y)
		{
			printf("0\n");
			return;
		}
		q[++cnt]=p[i];
		i=j;
	}
	ll ans=ksm(c,(q[1].x-1)*2)*ksm(c,(n-q[cnt].x)*2)%mo;
	fu(i,1,cnt)
	{
		ans=ans*calc(q[i+1].x-q[i].x)%mo;
	}
	printf("%lld\n",ans);
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;scanf("%d",&T);
	while(T--) work();
}
