#include<bits/stdc++.h>
#define ll long long

using namespace std;

const ll N = 1e6 + 10, mod = 1e9 + 7;

ll read(){
	char c = getchar(); ll x = 0, s = 1;
	while(!isdigit(c)) {if(c == '-') s = -1; c = getchar();}
	while(isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
	return x * s;
}

ll f[N][2], c[N], d[N];

ll is[N];

void solve(){
	ll n = read(), m = read(), v = read();
	memset(is, 0, sizeof(is));
	for(ll i = 1; i <= m; ++i) {
		ll x = read(), y = read();
		if(is[x] && is[x] != y) return cout << 0 << "\n", void();
		is[x] = y;
	}
	memset(f, 0, sizeof(f));
	if(is[1]) f[1][1] = 1;
	else f[1][0] = 1;
	for(ll i = 2; i <= n; ++i) {
		if(!is[i]) {
			f[i][1] = f[i - 1][1] * v % mod;
			f[i][0] = ((f[i - 1][1] * (v - 1) % mod * v % mod) + (f[i - 1][0] * v  % mod * v % mod)) % mod;
		} 
		else {
			f[i][1] = ((f[i - 1][1] * (1 + (v - 1) * v % mod) ) % mod + (f[i - 1][0] * v % mod * v % mod) % mod ) % mod;
		}
	}   
	cout << f[n][1] + f[n][0] << "\n";
}

int main() { 
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ll T = read();
	while(T --) solve();

	return 0;
} 
