#include #include #include #include #include #include #include #include #include #include #include #include typedef long long ll; typedef double lf; typedef unsigned long long ull; namespace FastIO { const int MAXSIZE = (1 << 20); char buf[MAXSIZE], *p1, *p2; #define gc (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? EOF : *p1++) template inline void Read(T &x) { x = 0; char ch = gc; bool f = 0; while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = gc; } while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = gc; if (f) x = -x; } } using FastIO::Read; using namespace std; const int MAXN = 1e5 + 10, mod = 1e9 + 7; inline ll Qpow(ll x, ll y) { ll res = 1; while (y) (y & 1) && (res = res * x % mod), x = x * x % mod, y >>= 1; return res; } int n, m; ll v; pair a[MAXN]; int main() { // freopen("day0/assign/assign3.in", "r", stdin); freopen("assign.in", "r", stdin); freopen("assign.out", "w", stdout); ios::sync_with_stdio(0), cin.tie(0); int T; Read(T); while (T--) { Read(n), Read(m), Read(v); for (int i = 1; i <= m; i++) Read(a[i].first), Read(a[i].second); sort(a + 1, a + m + 1); bool flag = 0; for (int i = 1; i < m; i++) if (a[i].first == a[i + 1].first && a[i].second != a[i + 1].second) { flag = 1; break; } if (flag) { cout << "0\n"; continue; } ll ans = 1; for (int i = 1; i < m; i++) if (a[i].first != a[i + 1].first) { int k = a[i + 1].first - a[i].first - 1; ans = ans * (Qpow(v, 2 * (k + 1)) - Qpow(v, k) * (v - 1) % mod) % mod; } if (a[1].first != 1) ans = ans * Qpow(v, 2 * (a[1].first - 1)) % mod; if (a[m].first != n) ans = ans * Qpow(v, 2 * (n - a[m].first)) % mod; cout << (ans % mod + mod) % mod << "\n"; } return 0; }