#include<bits/stdc++.h>
#define ll long long 
#define endl putchar('\n')
#define ygkg putchar(' ')
const ll inf=10000000000000000;
const ll mod=1000000007;
namespace io
{
	template<typename LX>
	void in(LX &ygs)
	{
		ygs=0;bool pdfh=0;char ch=getchar();
		while(!isdigit(ch)) pdfh=ch=='-',ch=getchar();
		do{ygs=(ygs<<3)+(ygs<<1)+(ch^48),ch=getchar();}while(isdigit(ch));
		if(pdfh) ygs=-ygs;
	}
	template<typename LX>
	void _out(LX ygs){if(ygs) _out(ygs/10),putchar((ygs%10)^48);return;}
	template<typename LX>
	void out(LX ygs){_out((ygs>0)?(ygs):(ygs?(putchar('-'),-ygs):(putchar('0'),0)));return;}
}
using namespace io;
using namespace std;
ll T;
ll n,m,v,ans;
struct aa{ll c,d;}f[100015];
bool cmp(aa xx,aa yy){return xx.c<yy.c||(xx.c==yy.c&&xx.d<yy.d);}
ll ksm(ll aa,ll bb)
{
	ll sum=1;
	for(;bb;bb>>=1)
	{
		if(bb&1) sum=(sum*aa)%mod;
		aa=(aa*aa)%mod;
	}
	return sum;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	in(T);
	while(T--)
	{
		in(n),in(m),in(v);
		for(ll i=1;i<=m;++i) in(f[i].c),in(f[i].d);
		sort(f+1,f+m+1,cmp);
		ans=ksm(v,2*(f[1].c-1));
		for(ll i=2;i<=m;++i)
		{
			if(f[i].c==f[i-1].c)
			{
				if(f[i].d!=f[i-1].d){ans=0;break;}
			}
			else
			{
				ans=(ans*(ksm(v,2*(f[i].c-f[i-1].c))-(ksm(v,f[i].c-f[i-1].c-1)*(v-1))%mod+mod))%mod;
			}
		}
		ans=(ans*ksm(v,2*(n-f[m].c)))%mod;
		out(ans),endl;
	}
	return 0;
}
