#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define R(i, x, y) for (int i = (x); i >= (y); i--)
#define FIO(FILE) freopen(FILE".in", "r", stdin), freopen(FILE".out", "w", stdout);
#define int long long

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
bool Mbe;

const int N = 1e5 + 5;
const int P = 1e9 + 7;

int n, m, v;
map<int, int> A;

int qpow(int p, int q) {
	int res = 1;
	for (; q; q >>= 1, p = p * p % P)
		if (q & 1) res = res * p % P;
	return res;
}

struct M {
	
	int s[2][2];
	
	M () { memset(s, 0, sizeof(s)); }
	M operator * (const M &rh) const {
		M res;
		F (i, 0, 1) F (j, 0, 1) F (k, 0, 1) {
			(res.s[i][j] += s[i][k] * rh.s[k][j] % P) %= P;
		}
		return res;
	}
	
};

M qp(M r, M p, int q) {
	M res = r;
	for (; q; q >>= 1, p = p * p)
		if (q & 1) res = res * p;
	return res;
}

int f(int n) {
	if (n == 0) {
		return 1;
	} else if (n == 1) {
		return (v * (v - 1) + 1) % P;
	} else {
		n--;
		M r, p;
		r.s[0][0] = (v * (v - 1) + 1) % P;
		r.s[0][1] = v * v % P * v % P;
		p.s[0][0] = v;
		p.s[1][0] = v - 1;
		p.s[1][1] = v * v % P;
		return qp(r, p, n).s[0][0];
	}
}

void solve() {
	cin >> n >> m >> v;
	A.clear();
	F (i, 1, m) {
		int c, d;
		cin >> c >> d;
		if (A[c] && A[c] != d) {
			cout << 0 << '\n';
			return;
		}
		A[c] = d;
	}
	if (m == 0) {
		cout << qpow(v, 2 * (n - 1)) << '\n';
		return;
	}
	int res = 1, lst = 0;
	for (auto p : A) {
		int i = p.fi;
		res = res * qpow(v, 2 * (i - 1)) % P;
		break;
	}
	for (auto p : A) {
		int i = p.fi;
		lst = i;
	}
	res = res * qpow(v, 2 * (n - lst)) % P;
	lst = 0;
	for (auto p : A) {
		int i = p.fi;
		if (lst) {
			res = res * f(i - lst) % P;
		}
		lst = i;
	}
	cout << res << '\n';
}

bool Med;
signed main() {
	FIO("assign");
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cerr << (&Mbe - &Med) / 1048576. << " MB\n";
	int T = 1;
	cin >> T;
	while (T--) solve();
	cerr << 1. * clock() / CLOCKS_PER_SEC << " ms\n";
	return 0;
}
