#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

template<typename T>
inline void read(T &x)
{
	x=0;
	T w=1;
	char c=getchar();
	while(c<'0'||c>'9') w=(c=='-'?-w:w),c=getchar();
	while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	x*=w;
}

const int S=100005;

#define p 1000000007

struct node
{
	int a,b;
};

int n,m,v;
node a[S];

inline int qpow(int x,int y)
{
	int res=1;
	for(;y>0;y>>=1,x=1ll*x*x%p) res=y&1?1ll*res*x%p:res;
	return res;
}

inline void add(int &x,int y)
{
	x+=y;
	if(x>=p) x-=p;
}

inline void slove()
{
	read(n),read(m),read(v);
	for(int i=1;i<=m;i++) read(a[i].a),read(a[i].b);
	sort(a+1,a+m+1,[&](node x,node y){return x.a<y.a;});
	int ans=1;
	for(int i=1;i<=m-1;i++)
	{
		if(a[i].a==a[i+1].a)
		{
			if(a[i].b!=a[i+1].b) return puts("0"),void();
			continue;
		}
		int len=a[i+1].a-a[i].a;
		int pre=qpow(v,len*2);
		add(pre,p-1ll*qpow(v,len-1)*(v-1)%p);
		ans=1ll*ans*pre%p;
	}
	ans=1ll*ans*qpow(v,(a[1].a-1)*2)%p;
	ans=1ll*ans*qpow(v,(n-a[m].a)*2)%p;
	printf("%d\n",ans);
}

int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	read(T);
	while(T-->0) slove();
	return 0;
}
