#include<bits/stdc++.h>
using namespace std;
#define debug 0
const int M = 1e9 + 7;
int T, n, m, v;
pair<int, int>p[100003];
int qpow(int a, int b){
	int res = 1;
	while (b > 0){
		if (b & 1) res = 1ll * res * a % M;
		a = 1ll * a * a % M;
		b >>= 1;
	}
	return res;
}
int main(){
	#if debug
		freopen("assign3.in", "r", stdin);
		freopen("assign3.out", "w", stdout);
	#else
		freopen("assign.in", "r", stdin);
		freopen("assign.out", "w", stdout);
	#endif
	scanf ("%d", &T);
	while (T--){
		scanf ("%d%d%d", &n, &m, &v);
		for (int i = 1;i <= m;++i)
			scanf ("%d%d", &p[i].first, &p[i].second);
		sort(p + 1, p + m + 1);
		int ans = 1;
		for (int i = 1;i <= m + 1;++i){
			if (i == 1) {
				int L = p[i].first - 1;
				ans = 1ll * ans * qpow(v, L * 2) % M;
			}
			else if (i == m + 1) {
				int L = n - p[i - 1].first;
				ans = 1ll * ans * qpow(v, L * 2) % M;
			}
			else {
				int L = p[i].first - p[i - 1].first;
				if (L == 0) {
					if (p[i].second != p[i - 1].second){
						ans = 0;
						break;
					}
				}
				else ans = 1ll * ans * (qpow(v, L * 2) + M - 1ll * qpow(v, L - 1) * (v - 1) % M) % M;
			}
		}
		printf ("%d\n", ans);
	}
	return 0;
} 
