#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
map <int , bool> mp;
unordered_map <int , int> t;
int power (int x , int y)
{
	int ans = 1;
	x %= mod;
	while (y)
	{
		if (y & 1)
			ans = ans * x % mod;
		y >>= 1;
		x = x * x % mod;
	}
	return ans;
}
void work ()
{
	mp.clear () , t.clear ();
	int n , m , v;
	cin >> n >> m >> v;
	bool et = 0;
	for (int i = 1;i <= m;i ++)
	{
		int c , d;
		cin >> c >> d;
//		cout << c << ' '  << d<< endl;
		if (mp[c] && t[c] != d)
		{
			cout << "0\n";
			et = 1;
		}
		mp[c] = 1;
		t[c] = d;
	}
	if (et)
		return ;
	int cnt = 0 , lst = -mod;
	int ans = 0;
	for (auto it : mp)
	{
		if (it.first == lst + 1)
			cnt ++;// , cout << it.first << ' ';
		lst = it.first;
	}
	ans = power (v * (v - 1) + 1 , cnt) * power (v * v , n - cnt - 1) % mod;
	cout << ans << '\n';
	
}

signed main ()
{
	ios::sync_with_stdio (0) , cin.tie (0) , cout.tie (0);
	freopen ("assign.in" , "r" , stdin);
	freopen ("assign.out" , "w" , stdout);
	int t;
	cin >> t;
	while (t --)
		work ();
	return 0;
}
