#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 9;
const ll mod = 1e9 + 7;
ll t, n, m, v, a[N];

inline bool chka()
{
	for (int i = 1; i <= n; ++ i)
		if (a[i] == 0)
			return false;
	return n == m;
}
inline void ta()
{
	cout << 0;
	ll ans = 1;
	for (int i = 1; i <= n; ++ i)
	{
		ans = ans * (v * (v - 1) + 1) % mod;
	}
	cout << ans << endl;
}
inline bool chkb()
{
	for (int i = 1; i <= n; ++ i)
		if (a[i] != 1 && a[i] != 0)
			return false;
	return true;
}
inline void tb()
{
	return ;
}
inline void td()
{
	return ;
}
int main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	cin >> t;
	while (t --)
	{
		memset(a, 0, sizeof(a));
		cin >> n >> m >> v;
		for (ll i = 1, c, d; i <= m; ++ i)
		{
			cin >> c >> d;
			a[c] = d;
		}
		if (chka())
			ta();
		else if (chkb())
			tb();
		else td();
	}
	return 0;
}
