#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
const int M=1000000007;
map<int ,int > ma;
int T,n,m,v,ans;
bool f;
int main()
{
	ios::sync_with_stdio(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	while(T--)
	{
		ans=1;
		f=0;
		ma.clear();
		cin>>n>>m>>v;
		for(int i=1;i<=m;i++)
		{
			int a,b;
			cin>>a>>b;
			if(ma[a]!=0&&ma[a]!=b)
			  f=1;
			ma[a]=b;
		}
		if(f)
		{
			cout<<0<<"\n";
			continue;
		}
		for(int i=1;i<n;i++)
		{
		    if(!ma[i]||!ma[i+1])
		      ans=ans%M*v%M*v%M%M;
		    else
		      ans=ans%M*(v%M*v%M-v%M+1)%M;
		}
		cout<<ans<<"\n";
	}
}
