#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int M = 1e9 + 7;
int t,x,y;
ll n,m,v,c[100005],xb[100005],ans;
bool fl;
ll ksm(ll a,ll tim)
{
	ll r = 1;
	while(tim)
	{
		if(tim % 2 == 0)
		{
			r = r * a % M;
		}
		tim /= 2;
		a = a * a % M;
	}
	return r;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lld%lld%lld",&n,&m,&v);
		fl = 0;
		for(int i = 1;i <= m;i++)
		{
			scanf("%d%d",&x,&y);
			xb[i] = x;
 			if(c[x] == 0)
			{
				c[x] = y;
			}
			else
			{
				fl = 1;
			}
		}
		if(fl)
		{
			cout << "0" << "\n";
			continue;
		}
		ans = ksm(v,n);
		cout << ans << "\n";
	}
	return 0;
}
