#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
#define db double
using namespace std;
const int inf=0x3f3f3f3f;
const ll lnf=1e18+10;
const db eps=1e-6;
ll read() {
	ll o=0,w=1;char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') w=-1;c=getchar();}
	while(c>='0'&&c<='9') {o=o*10+c-'0';c=getchar();}
	return o*w;
}
namespace dada {

const int N=1e5+10;
const ll P=1e9+7;
ll n,m,v,a[N],b[N],p[N];
struct Qpow {
	ll X,B,mt[N],Mt[N];
	void init(ll x) {
		X=x;
		B=sqrt(P);
		mt[0]=1;
		for(ll i=1;i<=B;i++) mt[i]=mt[i-1]*X%P;
		Mt[0]=1;
		for(ll i=1,j=B;j<=P;i++,j+=B) Mt[i]=Mt[i-1]*mt[B]%P;
	}
	ll Query(ll k) {
		k%=(P-1);
		return Mt[k/B]*mt[k%B]%P;
	}
}Q1,Q2;
int Main() {
	n=read(),m=read(),v=read();
	for(int i=1;i<=m;i++) {
		a[i]=read(),b[i]=read(),p[i]=i;
	}
	sort(p+1,p+1+m,[&](int x,int y) {
		return a[x]<a[y];
	});
	Q1.init((v*v)%P);
	Q2.init(v);
	ll Ans=Q1.Query(a[p[1]]-1)*Q1.Query(n-a[p[m]])%P;
	for(int i=1;i<m;i++) {
		ll x=a[p[i]],y=a[p[i+1]];
		if(x==y) {
			if(b[p[i]]!=b[p[i+1]]) Ans=0;//,cerr<<"NO\n";
		} else {
			Ans=Ans*((Q1.Query(y-x)-Q2.Query(y-x-1)*(v-1)%P+P)%P)%P;
		}
	}
	printf("%lld\n",Ans);
	return 0;
}



}
int main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T=read();
	while(T--) {
		dada::Main();
	}
	return 0;
}
/*
cd assign
g++ assign.cpp -o assign -std=c++14 -Wall -O2
time ./assign
cp assign1.in assign.in
diff assign.out assign1.ans -w
*/