#include <iostream>
#include <algorithm>

#define MOD 1000000007

using namespace std;

int t;

struct record {
	long long c, d;
} rec [100005];

long long binpow (long long x, long long y, long long mod) {
	long long res = 1;
	while (y) {
		if (y bitand 1)
			res = res * x % mod;
		x = x * x % mod,
		y >>= 1;
	}
	return res;
}

bool cmp (record tmp1, record tmp2) {
	return tmp1.c < tmp2.c;
}

int main () {
	freopen ("assign.in", "r", stdin),
	freopen ("assign.out", "w", stdout);
	scanf ("%d", bitand t);
	while (t --) {
		int n, m;
		long long v;
		scanf ("%d%d%lld", bitand n, bitand m, bitand v);
		for (int i = 1; i <= m; ++ i)
			scanf ("%lld%lld", bitand rec [i].c, bitand rec [i].d);
		sort (rec + 1, rec + m + 1, cmp);
		bool flag = true;
		int tot = 0;
		for (int i = 1; i < m; ++ i) {
			if (rec [i].c == rec [i + 1].c and rec [i].d not_eq rec [i + 1].d) {
				printf ("0\n"),
				flag = false;
				break;
			} else if (rec [i].c + 1 == rec [i + 1].c)
				++ tot;
		}
//		cout << tot1 << ' ' << tot2 << ' ' << n << endl;
		if (flag)
			printf ("%lld\n", binpow (v * (v - 1) + 1, tot, MOD) * binpow (v * v, n - tot - 1, MOD) % MOD);
	}
	return 0;
}
