#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 20;
const int mod = 1e9 + 7;

int fpow(int a, int b, int p) {
	if (b == 0)
		return 1;
	int ans = fpow(a, b / 2, p);
	ans = 1ll * ans * ans % p;
	if (b % 2 == 1)
		ans = 1ll * a * ans % p;
	return ans; 
}


struct Node {
	int x, y;
	Node (int _x = 0, int _y = 0) :
		x(_x), y(_y) {}
} a[N];
bool cmp(Node u, Node v) {
	return u.x == v.x ? u.y < v.y : u.x < v.x;
}
int n, m, v;

void read(int &x) {
	x = 0;
	char ch = getchar();
	while (ch < '0' || ch > '9')
		ch = getchar();
	while (ch >= '0' && ch <= '9')
		x = x * 10 + ch - '0', ch = getchar(); 
}

void slv() {
	read(n), read(m), read(v);
	for (int i = 1; i <= m; i++) 
		read(a[i].x), read(a[i].y);
	sort(a + 1, a + m + 1, cmp);
	int ans = 1, lst = 0;
	//不可行当且仅当能从fixed走到fixed
	for (int i = 1, j = 1; i <= m; i++) {
		if (i == m || a[i].x != a[i + 1].x) {
			if (a[j].y != a[i].y) {
				printf("0\n");
				return;
			}
			int x = a[i].x;
			if (lst == 0) {
				if (x > 1) {
					ans = fpow(v, 2 * (x - 1), mod);
				}
			}
			else {
				int res = fpow(v, 2 * (x - lst), mod);
				res = (res - 1ll * (v - 1) * fpow(v, x - lst - 1, mod) % mod + mod) % mod;
				ans = 1ll * ans * res % mod;
			}
			if (i == m) {
				ans = 1ll * ans * fpow(v, 2 * (n - x), mod) % mod;
			} 
			lst = x;
			j = i + 1;
		}
	}
	printf("%d\n", ans);
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int T;
	scanf("%d", &T);
	while (T--)
		slv();	
	return 0;
}
 
