#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int N = 1e6 + 5;
const ll mod = 1e9 + 7;

ll Read() {
	ll x = 0, f = 1; char c = getchar();
	while (c != '-' && !(c >= '0' && c <= '9')) c = getchar();
	if (c == '-') f = -f, c = getchar();
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}

namespace Main {
	ll n, m, v;
	ll mul (ll a, ll b) { return a * b % mod; }
	ll add (ll a, ll b) { return (a+b>mod)? a+b-mod: a+b; }
	ll dec (ll a, ll b) { return (a<b)? a-b+mod: a-b; }
	ll qpow (ll a, ll b) {
		if (b < 0) return 1;
		ll ans = 1;
		for (; b; b >>= 1, a = mul(a, a))
			if (b & 1) ans = mul(ans, a);
		return ans;
	}
	struct node{
		ll c, d;
		bool operator < (const node &a) const {
			return c < a.c;
		}
	}a[N];
	int main() {
		for (int T = Read(); T--; ) {
			n = Read(), m = Read(), v = Read(); 
			for (int i = 1; i <= m; i++) a[i].c = Read(), a[i].d = Read();
			sort (a + 1, a + 1 + m);
			bool flag = 0;
			int der = 0;
			for (int i = 1; i <= m - der; i++) {
				a[i] = a[i + der];
				for (; a[i].c == a[i + der + 1].c; der++)
					if (a[i].d != a[i + der + 1].d) { flag = 1; break; }
				if (flag) break;
			}
			m -= der;
			if (flag) { puts("0"); continue; }
			ll f = 1;
			for (int i = m; i; i--) {
				if (i == m) {
//					if (a[i].c == n) continue;
					f = qpow(v, mul(2ll, n - a[i].c));
				} else {
					ll len = a[i + 1].c - a[i].c - 1;
					f = add(qpow(v-1, dec(mul(2, n-(a[i + 1].c-1)), 1)), f); 
					if (len > 0) 
						f = add(qpow(v, add(len, len)), mul(dec(f, mul(v, v)), qpow(v, len - 1)));
				}
			}
			ll len = a[1].c - 2;
			if (len == 0) f = add(qpow(v-1, dec(mul(2, n-(a[1].c-1)), 1)), f);
			if (len > 0) {
				f = add(qpow(v-1, dec(mul(2, n-(a[1].c-1)), 1)), f);
				f = add(qpow(v, mul(2ll, len)), mul(dec(f, mul(v, v)), qpow(v, len - 1)));
			}
			printf ("%lld\n", f);
//			ll f1 = 1, f2 = 1, ans = 0;
//			for (int i = m; i; i--) {
//				if (i == m) {
//					if (a[i].c == n) continue;
//					f2 = mul(mul(v-1, v), qpow(mul(v, v), n - a[i].c - 1));
//					f1 = mul(qpow(v, n - a[i].c));
//				} else {
//					ll len = a[i + 1].c - a[i].c - 1;
//					if (len == 0) {
//						f2 = mul(mul(v-1, v), f2);
//						continue;
//					}
//					f2 = mul(mul(mul(v-1, v), qpow(mul(v, v), len)), add(f2, f1));
//					f1 = mul(qpow(v, len), f1);
//				}
//			}
//			printf ("%lld\n", mul(add(f1, f2), qpow(mul(v, v), a[1].c - 1)));
		}
		return 0;
	}
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	Main::main();
	return 0;
}

