#include<cstdio>
#include<algorithm>
const int N=1e5+1, MOD=1e9+7;

struct Query {
	int c, d;
}q[N];

long long n, m, v;

long long qpow(long long x, int y) {
	long long res=1;
	while(y) {
		if(y&1) {
			res=res*x%MOD;
		}
		x=x*x%MOD;
		y>>=1;
	}
	return res;
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int t;
	scanf("%d", &t);
	while(t--) {
	bool A=1, B=1;
	scanf("%lld %lld %lld", &n, &m, &v);
	for(int i=0; i<m; i++) {
		scanf("%d %d", &q[i].c, &q[i].d);
		if(q[i].c!=i+1) {
			A=0;
		}
	}
	
	
	std::sort(q, q+m, [](Query x, Query y) {
		return x.c<y.c;
	});
	long long ans=1ll*v*v%MOD;
	ans=qpow(ans, n-1);
	if(m==1) {
		printf("%lld\n", ans);
		continue;
	} else if(A&&m==n) {
		for(int i=0; i<n; i++) {
			(ans-=qpow(1ll*v*v, i)*(v-1))%=MOD;
			(ans+=MOD)%=MOD;
		}
	}
	printf("%lld\n", (long long)ans);
	}
}
