#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int M = 1e5 + 10;
int In[M][2];
int d[M];
const int mod = 1e9 + 7;
int qpow (int a, int b) {
	int ans = 1;
	while (b) {
		if (b & 1)
			ans = 1ll * ans * a % mod;
		a = 1ll * a * a % mod;
		b >>= 1;
	}
	return ans;
}
struct Node {
	int r, x;
} w[M];
bool cmp (Node a, Node b) {
	return a.x < b.x;
}
int n, m, v;
int A[15], b[15];
int x[15];
int ans;
void dfs (int dep1, int dep2) {
	if (dep1 >= n && dep2 >= n) {
		bool r = 1;
		for (int i = 1; i <= m; i ++)
			x[In[i][0]] = In[i][1];
		for (int i = 1; i < n; i ++) {
			if (x[i] == A[i] && x[i + 1] != b[i]) {
				r = 0;
				break;
			}
		}
		if (r) ans ++;
		return;
	}
	if (dep1 != n) {
		A[dep1] = 1;
		dfs(dep1 + 1, dep2);
		A[dep1] = 2;
		dfs(dep1 + 1, dep2);
	} else {
		b[dep2] = 1;
		dfs(dep1, dep2 + 1);
		b[dep2] = 2;
		dfs(dep1, dep2 + 1);
	}
}
signed main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	int T;
	cin >> T;
	while (T --) {
		ans = 0;
		cin >> n >> m >> v;
		if (m == 1) {
			int a, b;
			cin >> a >> b;
			if (b > v) {
				cout << "0\n";
				continue;
			}
			cout << qpow(v, 2 * n - 2) << '\n';
			continue;
		}
		bool A = 1, B = 1;
		for (int i = 1; i <= m; i ++) {
			cin >> In[i][0] >> In[i][1];
			w[i].x = In[i][0];
			w[i].r = In[i][1];
			if (In[i][0] != i) A = 0;
			if (In[i][1] != 1) B = 0;
		}
		bool G = 0;
		sort(w, w + m + 1, cmp);
		for (int i = 1; i < m; i ++)
			if (w[i].x == w[i + 1].x && w[i].r != w[i + 1].r) {
				cout << "0\n";
				G = 1;
				break;
			}
		if (G)
			continue;
		if (n == m && A) {
			bool InA = 1;
			for (int i = 1; i <= m; i ++) {
				if (In[i][1] > v) {
					cout << "0\n";
					InA = 0;
					break;
				}
			}
			if (InA)
				cout << qpow(v * v + 1 - v, n - 1) << '\n';
			continue;
		}
		if (v <= 2) {
			dfs(1, 1);
			cout << ans << '\n';
			continue;
		}
		if (B) {
			bool InB = 1;
			for (int i = 1; i <= m; i ++) {
				if (In[i][1] > v) {
					cout << "0\n";
					InB = 0;
					break;
				}
			}
			if (InB) {
				int ans1 = 0, ans2 = 0;
				for (int i = 1; i <= m; i ++)
					d[i] = In[i][0];
				sort(d + 1, d + m + 1);
				for (int i = 1; i < m; i ++)
					if (d[i] + 1 == d[i + 1])
						ans1 ++;
				ans2 = n - 1 - ans1;
				cout << 1ll * qpow(v * v - v + 1, ans1) * (v, 2 * ans2) % mod << '\n';
			}
			continue;
		}
		cout << "0\n";
	}
	return 0;
}
