#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10,mod = 1e9 + 7;
map <int,int> pl;
ll c[N],d[N];
ll n,m,v,T;
void c2c()
{
//	int ans = 0,is1 = 0,is2 = 0,is11,is12,is21,is22;
//	is11 = 
//	for(int i = 1;i <= m1 && is1 * is2 == 0;i++)
//		is1 = (d[i] == 1),is2 = (d[i] == 2);
}
ll qmi(ll x,ll y)
{
	ll ans = 1;
	while(y)
	{
		if(y & 1)
			ans *= x;
		x *= x;
		x %= mod;
		ans %= mod;
		y >>= 1;
	}
	return ans % mod;
}
void cnc()
{
	cout << qmi((v * v - v + 1) % mod,n - 1) % mod << '\n';
}
void c1c()
{
	cout << qmi(v,2 * n - 2) << '\n';
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
	cin >> T;
	int k;
	while(T--)
	{
		k = 0;
		cin >> n >> m >> v;
		pl.clear();
		for(int i = 1;i <= m;i++)
		{
			cin >> c[i] >> d[i];
			if(!pl[c[i]])
				pl[c[i]] = d[i];
			else if(!k) 
			{
				cout << 0 << '\n';
				k = 1;
			}
		}
		if(k)
			continue;
		 if(m == n)
		{
			cnc();
			continue;
		}
		else if(m == 1)
		{
			c1c();
			continue;
		}
		else
		{
			cout << 0 << '\n';
		}
	}
	return 0;
}
