#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const long long mo=1e9+7;
int t,n,m,x,y,p,vis,a[20],d[3][3];
long long v,ans;
struct cb{
	int x,d;
}c[100010];
bool cmp(cb x,cb y)
{
	 return x.x<y.x;
}
long long ksm(long long a,long long b)
{
	 long long sum=1;
	 while (b)
	 {
	 	   if (b&1) sum=sum*a%mo;
	 	   a=a*a%mo;
	 	   b=b/2;
	 }
	 return sum;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	while (t--)
	{
		  memset(a,0,sizeof(a));
		  memset(d,0,sizeof(d));
		  ans=0;
		  p=vis=1;
	      scanf("%d%d%lld",&n,&m,&v);
	      for (int i=1;i<=m;i++)
	      	  scanf("%d%d",&c[i].x,&c[i].d);
	      sort(c+1,c+m+1,cmp);
	      for (int i=1;i<=m;i++)
	      {
	          if (c[i].x==c[i-1].x&&c[i].d!=c[i-1].d) 
			  {
			     ans=-1;
				 break;
			  }
			  if (c[i].d!=1) vis=0;
			  if (c[i].x==c[i-1].x&&vis>0) vis=2;
			  if (c[i].x!=c[i].d) p=0;
		 }
		  if (ans==-1)
		  {
		  	 printf("0\n");
		  	 continue;
		  }
		  if (m==1)
		  {
		  	 ans=v*v%mo;
		  	 ans=ksm(ans,n-1);
		  	 printf("%lld\n",ans);
		  	 continue;
		  }
		  if (p)
          {
		  	 printf("1\n");
		  	 continue;
		  }
		  if (vis>0)
		  {
		  	 if (vis==1) 
		  	 {
		  	 	ans=n*n%mo;
		  	 	ans=ksm(ans,n-1);
		  	 	printf("%lld\n",ans);
			 }
			 if (vis==2)
			 {
			 	ans=(n*(n-1)%mo+1)%mo;
			 	ans=ksm(ans,n-1);
			 	printf("%lld\n",ans);
			 }
			 continue;
		  }
		if (v<=2)
		{
			for (int i=1;i<=m;i++)
			    a[c[i].x]=c[i].d;
			for (int i=1;i<=n;i++)
			    if (a[i]!=0&&a[i+1]!=0)
			       d[a[i]][a[i+1]]=1;
			if (d[1][2]==1&&d[1][1]==1||d[2][1]==1&&d[2][2]==1)
			{
			   printf("0\n");
			   continue;
		    }
		    if (d[1][1]+d[1][2]!=0&&d[2][1]+d[2][2]!=0)
		    {
		    	ans=2;
		    	ans=ksm(ans,n-1);
		    	printf("%lld\n",ans);
		    	continue;
			}
			if ((d[1][1]+d[1][2]==0)&&d[2][1]+d[2][2]==0)
			{
			   ans=4;
			   ans=ksm(ans,n-1);
			}
			else {
				ans=3;
				ans=ksm(ans,n-1);
			}
			printf("%lld\n",ans);
		}
	}
	return 0;
}
