#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

typedef long long i64;

const int SIZE = 1e5+5;
const int MOD = 1e9+7;

i64 n, m, v;
i64 c[SIZE], d[SIZE];

i64 qpow(i64 a, i64 b) {
	i64 res = 1;
	while (b) {
		if (b & 1) res = res * a % MOD;
		a = a * a % MOD;
		b >>= 1;
	}
	return res;
}

void solve() {
	map<i64, i64> mp;
	i64 ans = 1;
	scanf("%lld%lld%lld",&n,&m,&v);
	for (int i = 1; i <= m; ++i) {
		scanf("%lld%lld",c+i,d+i);
	}
	if (v == 1) {
		puts("1");
		return;
	}
	for (int i = 1; i <= m; ++i) {
		if (mp.count(c[i]) && mp[c[i]] != d[i]) {
			puts("0");
			return ;
		}
		mp[c[i]] = d[i];
	}
	i64 lst = 0;
	for (auto el : mp) {
		if (lst == 0) {
			ans *= qpow(v * v % MOD, el.first - lst - 1);
			ans %= MOD;
		} else {
			ans *= ((qpow(v, el.first - lst + 1) - v + 1) % MOD + MOD) % MOD;
			ans %= MOD;
			ans *= qpow(v, el.first - lst - 1);
			ans %= MOD;
		}
		lst = el.first;
	}
	ans *= qpow(v * v % MOD, n - lst);
	ans %= MOD;
	printf("%lld\n",ans);
}

int main() {
	#ifdef fio
//	freopen("../test.in","r",stdin);
	freopen("../day0/assign/assign3.in","r",stdin);
	freopen("../test.out","w",stdout);
	#else
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	#endif
	int t;
	scanf("%d",&t);
	while (t--) {
		solve();
	}
	return 0;
}
