#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9') {
		if(ch == '-') f = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}
#define pb push_back
#define fi first
#define se second
#define N 200010
//#define M
#define mo (int)(1e9 + 7)
int n, m, i, j, k, T; 
int ans, sum, v, a[N], flg; 
map<int, int>mp; 

int pw(int a, int b) {
	int ans = 1; 
	while(b) {
		if(b & 1) ans *= a; 
		a *= a; b >>= 1; 
		ans %= mo; a %= mo; 
	}
	return ans;  
}

signed main() {
//	srand(time(0));
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	T = read(); 
	while(T--) {
		n = read(); m = read(); v = read(); mp.clear(); flg = 0; 
		for(i = 1; i <= m; ++i) {
			a[i] = read(), k = read(); 
			if(mp[a[i]] > 0 && mp[a[i]] != k) flg = 1; 
			mp[a[i]] = k; 
		}
		if(flg) { printf("0\n"); continue; }
		sort(a + 1, a + m + 1); ans = pw(v, 2 * ((a[1] - 1) + (n - a[m]))); 
		for(i = 1; i < m; ++i) {
			if(a[i] == a[i + 1]) continue; 
			sum = pw(v, 2 * (a[i + 1] - a[i])) - pw(v, a[i + 1] - a[i] - 1) * (v - 1) % mo; 
			ans *= sum; ans %= mo; 
		}
		ans = (ans % mo + mo) % mo; 
		printf("%lld\n", ans); 
	}
	return 0;
}

