#include<iostream>
#include<cstdio>
using namespace std;
int t;
const int k=1e9+7;
struct abab{
	int x,y;
};
abab a[100005]={0};
bool cmp(abab x,abab y)
{
	return x.x<y.x;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	while(t--)
	{
		int n,m,v,ans=1;
		scanf("%d%d%d",&n,&m,&v);
		for(int i=1;i<=m;i++)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			a[i].x=x;a[i].y=y;
		}
		if(v==1)
		{
			cout<<ans<<endl;
			continue;
		}
//		sort(a+1,a+m+1,cmp);
		//a=b
		bool pd=0;
		for(int i=2;i<=m;i++)
		{
			if(a[i].x==a[i-1].x&&a[i].y!=a[i-1].y)
			{
				pd=1;
				cout<<"0"<<endl;
				break;
			}
			if(a[i].x!=a[i-1].x)
				pd=1;
		}
		if(!pd)
		{
			for(int i=1;i<=(n-1)*2;i++)
			{
				ans*=v;
				ans%=k;
			}
			cout<<ans<<endl;
		}
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
