#include <bits/stdc++.h>
using namespace std;
const int N = 5e5;
const int inf = 1e9;
typedef long long ll;
const ll INF = 1e15;
int read() {
	int s = 0,f = 1;char ch = getchar();
	while (!isdigit(ch)) f = ch == '-' ? -1 : 1, ch = getchar();
	while (isdigit(ch)) s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}
const int mod = 1e9 + 7;
int n,m,v,t[N + 10];
map<int,int> mp;
int qpow(int a,int b) {
	int res = 1;
	while (b) {
		if (b & 1) res = 1ll * res * a % mod;
		b >>= 1, a = 1ll * a * a % mod;
	}
	return res;
} 
int main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T = read();
	while (T -- ) {
		n = read(), m = read(), v = read();
		int fl = 0;
		mp.clear();
		for (int i = 1;i <= m;i ++ ) {
			int x = read(),y = read();
			t[i] = x;
			if (!mp[x]) mp[x] = y;
			else {
				if (mp[x] != y) fl = 1;
			}
		}
		if (fl) {
			puts("0");
			continue;
		}
		sort(t + 1,t + m + 1);
		int ans = qpow(v,(t[1] - 1) * 2);
		ans = 1ll * ans * qpow(v,n - t[m] << 1) % mod;
		for (int i = 1;i < m;i ++ ) {
			if (t[i] == t[i + 1]) continue;
			int all = qpow(v,(t[i + 1] - t[i]) * 2),il = (v - 1ll) * qpow(v,t[i + 1] - t[i] - 1) % mod;
			ans = 1ll * ans * (all - il + mod) % mod;
		}
		printf("%d\n",ans);
	}	
	return 0;
}
