#include <bits/stdc++.h>
#define _rep(i_, a_, b_) for(int i_ = a_; i_ <= b_; ++i_)
int in(void) { int x; scanf("%d", &x); return x; }
typedef long long ll;
using namespace std;
const int kN = 100500, p = 1000000007;
int fpm(int a, int b) {
	int r = 1;
	for(;b;b >>= 1, a = 1ll * a * a % p) if(b & 1) r = 1ll * r * a % p;
	return r;
}
int s[kN];
int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int TC = in();
	while(TC--) {
		int n = in(), m = in(), v = in();
		map<int,int> a;
		bool flg = 0;
		_rep(i,1,m) {
			int x = in(), y = in();
			if(a.count(x) && a[x] != y) flg = 1;
			a[x] = y;
		}
		if(flg) {
			puts("0");
			continue;
		}
		int res = 1, N = 0;
		for(auto &x : a) s[++N] = x.first;
		if(s[1] > 1) res = 1ll * res * fpm(v, 2 * (s[1] - 1)) % p;
		_rep(i,1,N - 1) res = 1ll * res * (fpm(v, 2 * (s[i + 1] - s[i])) - 1ll * fpm(v, s[i + 1] - s[i] - 1) * (v - 1) % p + p) % p;
		if(s[N] < n) res = 1ll * res * fpm(v, 2 * (n - s[N])) % p;
		printf("%d\n", res);
	}
	return 0;
}

