#include<cstdio>
#include<algorithm>
using namespace std;
inline long long read()
{
	long long x(0),f(1); char ch(getchar());
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1; ch=getchar();
	} while(ch>='0'&&ch<='9')
		x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return x*f;
}
long long T,S,F,n,m,k,t;
const long long M(1e9+7);
struct acg{long long x,y;}a[100005];
bool cp(acg x,acg y){return x.x<y.x;}
long long ksm(long long x,long long y)
{
	long long s(1);
	while(y){
		if(y&1) s=s*x%M;
		x=x*x%M; y>>=1;
	} return s;
}
int main()
{
	freopen("assign.in","r",stdin); freopen("assign.out","w",stdout);
	T=read();
	while(T--){F=t=0; S=1;
		n=read(); m=read(); k=read();
		for(long long i=1;i<=m;i++)
			a[i].x=read(),a[i].y=read();
		sort(a+1,a+1+m,cp);
		for(long long i=2;i<=m;i++){
			if(a[i].x!=a[i-1].x)
				t=ksm(k,a[i].x-a[i-1].x-1)*(k-1)%M,
				S=(ksm(k,2*a[i].x-2*a[i-1].x)-t+M)%M*S%M;
			else if(a[i].y!=a[i-1].y){F=1; break;}
		} S=ksm(k,2*a[1].x-2)*ksm(k,2*n-2*a[m].x)%M*S%M;
		//printf("a %lld %lld ",S,ksm(k,2*n-2));
		if(F) printf("0\n");
		else printf("%lld\n",S);
	} return 0;
}
