#include <algorithm>
#include <iostream>
using namespace std;
const int N = 1e5 + 7;
const int O = 1e9 + 7;
typedef long long i64;

i64 fpow(i64 x, i64 k) {
	i64 r = 1;
	for(; k; k >>= 1, x = x * x %O)
		if(k & 1) r = r * x %O;
	return r;
}
#define inv(x) (fpow((x),O-2))

struct node {
	int p, v;
} lm[N], tmp[N];

bool operator < (const node& x, const node& y) {
	return x.p < y.p;
}

int n, m, V;
inline void solve() {
	cin >> m >> n >> V;
	for(int i = 1; i <= n; ++i)
		cin >> lm[i].p >> lm[i].v;
	sort(lm + 1, lm + n + 1);
	swap(lm, tmp);
	int xn = 1;
	lm[1] = tmp[1];
	for(int i = 2; i <= n; ++i) {
		if(tmp[i].p == tmp[i-1].p) {
			if(tmp[i].v != tmp[i-1].v){
				cout << 0 << "\n";
				return ;
			}
		} else lm[++xn] = tmp[i];
	}
	n = xn;
//	for(int i = 1; i <= n; ++i) 
//		cout << ":" << lm[i].p << ' ' << lm[i].v << "\n";
	i64 ans = fpow(V, 2 * (m - 1));
	for(int i = 1; i < n; ++i) {
		int l = lm[i].p, r = lm[i + 1].p;
		i64 all = fpow(V, 2 * (r - l));
		i64 ill = fpow(V, r - l - 1) * (V - 1) %O;
		ans = ans * (all - ill + O) %O * inv(all) %O;
	}
	cout << ans << "\n";
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t; cin >> t;
	for(; t--; ) solve();
}
