#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e6+10;
const int mod=1e9+7;
int n,m,v,a[maxn];

int qpow(int a,int b)
{
	int ret=0;
	for(;b;a=a*a%mod,b>>=1) if(b&1) ret=ret*a%mod;
	return ret; 
}

bool checkA()
{
	for(int i=1;i<=n;i++) if(a[i]!=i) return false;
	return true; 
}

void solve()
{
	cin>>n>>m>>v;
	int f=0;
	for(int i=1;i<=m;i++)
	{
		int x,y;
		cin>>x>>y;
		if(a[x]) f=1;
		a[x]=y;
	}
	int ans=0;
	if(f) return cout<<0<<endl,void();
	if(checkA()) return cout<<qpow(v-1,n)*qpow(v,n)%mod<<endl,void();
	cout<<ans<<endl;
}

signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T;
	cin>>T;
	while(T--) solve();
	return 0;
}
/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2
*/
