#include<bits/stdc++.h>
#define int long long
using namespace std;
const int M=1e5+5,MOD=1e9+7;
int n,m,v,dp[M];//dp[i]表示到了第i条限制（排了序的） 的答案个数 
struct uuu{
	int c,d;
}a[M];
bool cmp(uuu a,uuu b)
{
	return a.c<b.c;
}
int ksm(int a,int b)
{
	int ans=1;
	while(b)
	{
		if(b&1)
		{
			ans=ans%MOD*a%MOD;
		}
		a=((a%MOD)*(a%MOD))%MOD;
		b>>=1;
	}
	return ans;
}
void solve()
{
	cin>>n>>m>>v;
	for(int i=1;i<=m;i++)
	{
		cin>>a[i].c>>a[i].d;
	}
	sort(a+1,a+1+m,cmp);
	for(int i=1;i<=m;i++)
	{
		if(a[i].c==a[i-1].c && a[i].d!=a[i-1].d)
		{
			cout<<"0\n";
			return ;
		}
	}
	cout<<ksm(v*v,n-1)<<"\n";
}
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int _;
	cin>>_;
	while(_--)
	{
		solve();
	}
}
