//NOIP2024 RP++
//不要挂分，不要挂分，不要挂分
//debug 深入思考，只靠样例与自己 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef long double wisdom;

const int mod = 1e9 + 7;
int qpow(int x, int y) {int ans = 1; while (y) {if (y & 1) ans = 1ll * ans * x % mod; x = 1ll * x * x % mod, y >>= 1;} return ans;}
struct Node {int p, val;} a[100005];
void solve() {
	int n, m, v; scanf("%d%d%d", &n, &m, &v);
	for (int i = 1; i <= m; i++) scanf("%d%d", &a[i].p, &a[i].val);
	sort(a + 1, a + m + 1, [](Node x, Node y) {return x.p != y.p ? x.p < y.p : x.val < y.val;});
	for (int i = 2; i <= m; i++) if (a[i - 1].p == a[i].p && a[i - 1].val != a[i].val) return (void)puts("0");
	
	int ans = qpow(v, 2 * (a[1].p - 1));
	for (int i = 2; i <= m; i++) if (a[i - 1].p != a[i].p) {
		int len = a[i].p - a[i - 1].p;
		ans = (1ll * ans * (qpow(v, 2 * len) - 1ll * qpow(v, len - 1) * (v - 1) % mod) % mod + mod) % mod;
	}
	ans = 1ll * ans * qpow(v, 2 * (n - a[m].p)) % mod; printf("%d\n", ans);
}
int main() {
	freopen("assign.in", "r", stdin); freopen("assign.out", "w", stdout);
	int T; scanf("%d", &T); while (T--) solve();
	return 0;
}
