#include<bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e5 + 7, mo = 1e9 + 7;
int i, j, k, n, m, v;
int res;
struct pq {
	int c, d;
	#define c(i) d[i].c
	#define d(i) d[i].d
	bool operator < (const pq&x) const {return c < x.c;}
} d[N], e[N];

int qp(int x, int y) {
	int s = 1;
	for(; y; y>>=1, x=1LL*x*x%mo) if(y&1) s = 1LL*s*x%mo;
	return s;
}

int cal(int n) {
	if(n <= 0) return 0;
	int s = 0, i;
	for(i = 0; i <= n; i++) s = (s + qp(1LL*v*v%mo, i)) % mo;
	return s;
}

void sol() {
	scanf("%d%d%d", &n, &m, &v);
	for(i = 1; i <= m; i++) scanf("%d%d", &c(i), &d(i));
	sort(d + 1, d + 1 + m);
	for(i = 2; i <= m; i++) {
		if(c(i) == c(i-1) && d(i) != d(i-1)) {
			puts("0");
 			return;
		}
	}
	k = 0;
	for(i = 1; i <= m; i++) if(c(i) != c(i-1)) e[++k] = d[i];
	int res = qp(v, 2 * (e[1].c - 1));
	for(i = 1; i < k; i++) {
		int t = e[i+1].c - e[i].c;
		res = 1ll * res * (qp(v, 2 * t) - 1ll * (v - 1) % mo * qp(v, t-1) % mo + mo) % mo;
	}
	res = 1ll * res * qp(v, 2 * (n - e[k].c)) % mo;
	printf("%d\n", res);
}

signed main() {
//	freopen("1.in", "r", stdin);
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int T; scanf("%d", &T);
	while(T--) sol();
}
