#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n, m, v, sum, del, f, c[N];
map<int, int> mp; 
int qpow(int x, int y){
	int ret = 1;
	for (; y; x = x * x % mod){
		if (y & 1)
			ret = ret * x % mod;
		y >>= 1;
	}
	return ret % mod;
}
void init(){
	mp.clear();
	del = f = 0;
	for (int i = 0; i < m; i++)
		c[i] = 0;
}
signed main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while(T--){
		cin >> n >> m >> v;
		sum = qpow(v, 2 * n - 2);
		for (int i = 0, x, y; i < m; i++){
			cin >> x >> y;
			if (x == n)
				continue;
			if (mp[x] != y && mp[x] != 0)
				f = 1;
			c[i] = x;
			mp[x] = y;
		}
		if (f == 1){
			cout << "0\n";
			init();
			continue;
		}
		sort(c, c + m);
		for (int i = 0; i < m - 1; i++)
			if (c[i + 1] == c[i] + 1)
				del++;
		if(del > 0)
			cout << (qpow(v, 2 * n - 2 - del * 2) + qpow(v, 2 * n - 2 - del) * qpow (v - 1, del) % mod) % mod << "\n";
		else
			cout << sum << "\n";
		init();
	}
	return 0;
}
