#include<bits/stdc++.h>
using namespace std;
const long long MAXN=1010000,Moder_=1e9+7;
void read(long long &x)
{
	x=0;
	long long opt=1;
	char c=getchar();
	while(c<'0'&&c>'9')
	{
		if(c=='-')
		{
			opt=-1;
		}
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=(x*10+c-48);
		c=getchar();
	}
	x*=opt;
	return;
}
long long n,m,v,lis[MAXN],R;
map<long long,long long> book;
long long poww(long long ta,long long tb)
{
	long long ans=1;
	while(tb>0)
	{
		if(tb&1)
		{
			ans=(ans*ta)%Moder_;
		}
		ta=(ta*ta)%Moder_;
		tb/=2;
	}
	return ans;
}
long long calc(long long v,long long d)
{
	long long ta=poww(v,d*2),tb=poww(v,d-1),tc=(v-1),t;
	t=ta-tb*tc;
	t=((t%Moder_)+Moder_)%Moder_;
	return t;
}
void work()
{
	R=0;
	book.clear();
	long long i,ta,tb,flag=1,ans=1;
	read(n),read(m),read(v);
	for(i=1;i<=m;i++)
	{
		read(ta),read(tb);
		if(book[ta]==0)
		{
			book[ta]=tb;
			R++;
			lis[R]=ta;
		}
		else if(book[ta]!=tb)
		{
			flag=0;
		}
	}
	if(!flag)
	{
		printf("0\n");
		return;
	}
	sort(lis+1,lis+1+R);
	if(R==0)
	{
		printf("%lld\n",poww(v,2*n-2));
	}
	else
	{
		ans=(ans*poww(v,2*lis[1]-2))%Moder_;
		ans=(ans*poww(v,2*(n-lis[R]+1)-2))%Moder_;
		for(i=2;i<=R;i++)
		{
			ans=(ans*calc(v,lis[i]-lis[i-1]))%Moder_;
		}
		printf("%lld\n",ans);
	}
	return;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	long long T;
//	scanf("%lld",&T);
	read(T);
	while(T--)
	{
		work();
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
