#include <bits/stdc++.h>
using namespace std;
#define ll long long

ll const mod =1e9+7;
int t,n,m,v;
vector<int>si;
struct line 
{
	int i=0,x=0;
}a[100005];

bool cmp(line y,line z)
{
	return y.i<z.i;
}

ll powmod(ll x,ll y)
{
	ll ans = 1;
	while(y)
	{
		if(y%2==1)ans=ans*x%mod;
		y/=2;
		x=x*x%mod;
	}
	return ans%mod;
}

int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin >> t;
	while(t--)
	{
		cin >> n>>m>>v;
		for(int i = 1;i <= m;i++)
		{
			int x,y;
			cin >>x>>y;
			a[i].i=x;
			a[i].x=y;
		}
		sort(a+1,a+m+1,cmp);
		ll cnt = 0;
		ll ans = 1;
		bool f=false;
		for(int i = 1;i<m;i++)
		{
			while(a[i].i == a[i+1].i&&a[i].x==a[i+1].x&&i<=m)i++;
			if(i==m+1)break;
			if(a[i].i == a[i+1].i&&a[i].x!=a[i+1].x)
			{
				f = true;
				break;
			}
			if(a[i].i == a[i+1].i-1)cnt++;
		}
		if(f)
		{
			cout << 0<<endl;
			continue;
		}
		ans = ans*powmod(v,((n-1-cnt)%mod+mod-1)%(mod-1))%mod;
		ans = ans*powmod(v,((n-1-cnt)%mod+mod-1)%(mod-1))%mod;
		ans = ans*powmod((2*v%mod-1+mod)%mod,cnt%(mod-1))%mod;
		cout << ans<<endl;
	}
	
	return 0;
}
