#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9')
	{
		if (ch=='-')
			f=-1;
		ch=getchar();
	}
	while (ch>='0'&&ch<='9')
	{
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}

const ll MOD=1e9+7;
const int N=1e5+10;
int T,m,c,d,ans,fff;
int n,v;
int a[N],b[N],x[N],vis[N],k[N];

inline void work2()
{
	for (int i=1;i<=n;i++)
		k[i]=x[i];

	int f=0;
	for (int i=1;i<=n;i++)
		if (vis[i]==1){
			f=i;break;
		}
	if (f==0||f==n){
		fff=1;return ;
	}
	for (int i=f;i<=n;i++)
	{
		if (vis[i]&&i!=f)
		{
			if (k[i]==3)
				k[i]=x[i];
			else
			{
				if (k[i]!=x[i])
				{
					fff=0;
					return ;
				}
			}
		}
		
		if (k[i]==a[i])
			k[i+1]=b[i];
		if (k[i]!=a[i])
			k[i+1]=3;
	}
	fff=1;
	return ;
}

inline void ss(int p)
{
	if (p>=n)
	{
		fff=0;
		work2();
		if (fff)
		{
			ans=(ans%MOD+1)%MOD;
		}
		return ;
	}
	a[p]=1;
	b[p]=1;
	ss(p+1);
	
	a[p]=1;
	b[p]=2;
	ss(p+1);
	
	a[p]=2;
	b[p]=2;
	ss(p+1);
	
	a[p]=2;
	b[p]=1;
	ss(p+1);
}

inline void work()
{
	ans=fff=0;
	memset(vis,0,sizeof(vis));
	
	n=read(),m=read(),v=read();
	
	
	if (m==1)
	{
		c=read()%MOD,d=read()%MOD;
		unsigned ll pp1,pp2;
		pp1=pp2=(v%MOD*v%MOD)%MOD;
		for (int i=2;i<=n-1;i++)
		{
			pp1=(pp1%MOD*pp2%MOD)%MOD;
		}	
		printf("%u\n",pp1);
		return ;
	}
	if (v>2)
	{
		for (int i=1;i<=m;i++)
			c=read()%MOD,d=read()%MOD;
		printf("0\n");
		return ;
	}
	
	int flag=0;
	for (int i=1;i<=m;i++)
	{
		c=read(),d=read();
		if (vis[c]&&x[c]!=d)
		{
			flag=1;
			break;
		}
		x[c]=d;
		vis[c]=1;
	}
	
	if (flag){
		printf("0\n");
		return ;
	}
	
	ss(1);
	
	printf("%d\n",ans%MOD);
	
	return ;
}

int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);

	T=read();
	while (T--)
		work();
	return 0;
} 

/*

3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2



*/

