#include<map>
#include<cstdio>
#include<memory.h>
#include<algorithm>
typedef long long ll;
#define fr(i,a,b) for(int i=a;i<=b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
constexpr int M=1e5,MOD=1e9+7;
ll ksm(ll x,ll y=MOD-2)
{
	ll res=1;
	while(y)
	{
		if(y&1) res=res*x%MOD;
		x=x*x%MOD;
		y>>=1;
	}
	return res;
}
int T;
ll n,m,v;
struct XJ
{
	int c,d;
}p[M+5];
ll f[2*M+5][2];
ll s[2*M+5];
map<int,bool>vis;
ll getsum(int x)
{
	return (ksm(v,x+1)-1+MOD)%MOD*ksm(v-1)%MOD;
}
ll power(int l,int r)
{
	return (getsum(r)-getsum(l-1)+MOD)%MOD;
}
int ind[M+5];
void solve()
{
	vis.clear();
	memset(f,0,sizeof(f));
	memset(s,0,sizeof(s));
	fr(i,1,m) vis[p[i].c]=1;
	fr(i,1,m) ind[i]=p[i].c;
	int len=unique(ind+1,ind+1+m)-ind-1;
	ind[++len]=n+1;
	int st=1,tot=0;
	if(!vis[1])
	{
		int j=*lower_bound(ind+1,ind+1+len,2)-1;
		f[++tot][0]=ksm(v,2*j-2);
		st=j;
	}
	else
	{
		f[++tot][1]=1;
	}
	fr(i,st,n-1)
	{
		if(!vis[i+1])
		{
			int j=*lower_bound(ind+1,ind+1+len,i+1)-1;
			int cnt=j-i;
			++tot;
			f[tot][0]=f[tot-1][1]*power(cnt,2*cnt-1)%MOD*(v-1)%MOD;
			s[tot]=f[tot-1][1]*ksm(v,cnt-1)%MOD;
			i=j-1;
			continue;
		}
		++tot;
		(f[tot][1]+=f[tot-1][0]*v%MOD*v%MOD)%=MOD;
		f[tot-1][0]=0;
		(f[tot][1]+=(f[tot-1][vis[i]]+v*s[tot-1]%MOD)%MOD*(((v-1)*v+1)%MOD)%MOD)%=MOD;
	}
	printf("%lld\n",(f[tot][vis[n]]+v*s[tot]%MOD)%MOD);
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	fr(_,1,T)
	{
		scanf("%lld%lld%lld",&n,&m,&v);
		fr(i,1,m)
		{
			int c,d;
			scanf("%d%d",&c,&d);
			p[i]={c,d};
		}
		sort(p+1,p+1+m,[](XJ x,XJ y){return x.c<y.c;});
		bool flg=1;
		fr(i,1,m-1)
		{
			if(p[i].c==p[i+1].c&&p[i].d!=p[i+1].d)
			{
				puts("0");
				flg=0;
				break;
			}
		}
		if(flg) solve();
	}
	return 0;
}