#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;
const int Max = 100005;
const int Mod = int(1e9) + 7;

long long P[Max], P1[Max];

long long qPow(long long x, long long y) {
	// calc (x^y) % Mod
	long long res = 1;
	while (y) {
		if (y & 1) res = res * x % Mod;
		y >>= 1;
		x = x * x % Mod;
	}
	return res;
}

namespace Solve {
	int n, m, v;
	int dfsAns, x[15], y[15], a[15], b[15];
	
	struct CD {
		int c, d;
		
		bool operator<(const CD &cmp) const {
			return c < cmp.c;
		}
	} cd[Max];

	long long C(long long x, long long y) {
		// x > y
		return P[x] * P1[y] % Mod * P1[x - y] % Mod;
	}

	void solveA() {
		/*
			if a[i] == x[i] then
				b[i] = x[i + 1]
				{plans} = 1
			else 
				b[i] = 1, 2, 3, ..., v
				{plans} = v * (v - 1)
		*/

		long long ans = 0;
		long long p = 1;
		long long vv = (long long) v * (v - 1) % Mod;

		for (int i = n - 1; i >= 0; i--) {
			long long res = C(n - 1, i);
			ans = (ans + p * res % Mod) % Mod;
			p = p * vv % Mod;
		}

		printf("%lld\n", ans);
	}
	
	void solve0() {
		long long vv = (long long) v * v % Mod;
		printf("%lld\n", qPow(vv, n - 1));
	}
	
	void solveB() { // failure
		sort(cd + 1, cd + m + 1);
		int cnt = 0;
		for (int i = 1; i < m; i++) 
			cnt += (cd[i].c + 1 == cd[i + 1].c);
		
		long long ans = 0;
		long long vv = (long long) v * (v - 1) % Mod;
		long long p = qPow(vv, n - 1 - cnt);

		for (int i = cnt; i >= 0; i--) {
			long long res = C(cnt, i);
			ans = (ans + p * res % Mod) % Mod;
			p = p * vv % Mod;
		}

		printf("%lld\n", ans);
	} 
	
	void dfs(int i) {
		if (i == n) {
			memset(y, 0, sizeof y);
			for (int j = 1; j <= n; j++) {
				if (!y[j]) {
					if (x[j]) y[j] = x[j];
					else y[j] = a[j] % 2 + 1;
				}
				
				if (x[j] && y[j] != x[j]) return;
				if (y[j] == a[j]) y[j + 1] = b[j];
			}
			dfsAns++;
		} else {
			a[i] = 1, b[i] = 1; dfs(i + 1);
			a[i] = 1, b[i] = 2; dfs(i + 1);
			a[i] = 2, b[i] = 1; dfs(i + 1);
			a[i] = 2, b[i] = 2; dfs(i + 1);
		}
	}
	
	void bruteForce() {
		dfsAns = 0;
		memset(x, 0, sizeof x);
		
		sort(cd + 1, cd + m + 1);
		for (int i = 1; i < m; i++) 
			if (cd[i].c == cd[i + 1].c && cd[i].d != cd[i + 1].d) {
				puts("0");
				return;
			} else
				x[cd[i].c] = cd[i].d;
		
		dfs(1);
		printf("%d\n", dfsAns);
	}

	void solve() {
		scanf("%d %d %d", &n, &m, &v);

		bool flagA = true, flagB = true;
		flagA &= (n == m);

		for (int i = 1; i <= m; i++) {
			scanf("%d %d", &cd[i].c, &cd[i].d);
			flagA &= (cd[i].c == i);
			flagB &= (cd[i].d == 1);
		}

		if (m <= 1) {
			solve0();
			return;
		}
		if (flagA) {
			solveA();
			return;
		}
		if (n <= 12) {
			bruteForce();
			return;
		}
		if (flagB) {
			solveB();
			return;
		}
		
		puts("0");
	}
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);

	int T;
	scanf("%d", &T);

	P[0] = P1[0] = 1;
	for (int i = 1; i < Max; i++) {
		P[i] = P[i - 1] * i % Mod;
		P1[i] = qPow(P[i], Mod - 2);
	}

	while (T--) Solve::solve();
}
