// I love Furina forever
# include <bits/stdc++.h>
# define maxn 100100
# define mod 1000000007
# define ll long long
# define mem(a, val) memset(a, val, sizeof(a))
# define rep(i, j, k) for(ll i = j; i <= k; ++i)
# define per(i, j, k) for(ll i = j; i >= k; --i)
using namespace std;

ll T, n, m, val, ans;
ll a[maxn], cnt;
map<int, int> mp;

inline ll qp(ll a, ll b) {ll mul = 1; while(b) mul = mul * (b & 1 ? a : 1) % mod, a = a * a % mod, b >>= 1; return mul;}
inline ll Mod(int x) {
	if(x >= mod) return x % mod;
	if(x < 0) return (x % mod + mod) % mod;
	return x;
}

inline void solve() {
	mp.clear(); cnt = 0; ans = 1;
	cin >> n >> m >> val;
	rep(i, 1, m) {
		int c, d; cin >> c >> d;
		if(mp.count(c) && mp[c] != d) {cout << "0\n"; return;}
		mp[c] = d;
	}
	for(auto it = mp.begin(); it != mp.end(); it++) a[++cnt] = it -> first;
	rep(i, 2, cnt) {
		int len = a[i] - a[i - 1];
		ll tmp1 = qp(val, 2 * len), tmp2 = qp(val, len - 1) * (val - 1) % mod;
		ans = ans * Mod(tmp1 - tmp2) % mod;
	}
	ans = ans * qp(val, (a[1] - 1) * 2) % mod;
	ans = ans * qp(val, (n - a[cnt]) * 2) % mod;
	cout << ans << '\n';
}

signed main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> T;
	while(T--) solve();
	return 0;
}

