#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5+10;
const int mod = 1e9+7;
int T,n,m,v,d[N],c[N],x[N],ans;
int read()
{
	int x=0,w=1;
	char ch = getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch == '-') w=-1;
		ch =  getchar();
	}
	while(ch>='0' && ch<='9')
	{
		x = x*10+ch-'0';
		ch = getchar();
	}
	return x*w;
}
int qpow(int a,int b)
{
	int res = 1;
	while(b)
	{
		if(b&1) res = res*a%mod;
		a = a*a%mod;
		b >>= 1;
	}
	return (res+mod)%mod;
}
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	T=read();
	while(T--)
	{
		bool flag = true;
		ans = 0;
		n=read(); m=read(); v=read();
		for(int i = 1;i <= n;i++)
		{
			x[i] = 0;
		}
		for(int i = 1;i <= m;i++)
		{
			c[i]=read(); d[i]=read();
			if(x[c[i]]>0 && x[c[i]]!=d[i])
			{
				flag = false;
			}
			x[c[i]] = d[i];
		}
		if(!flag)
		{
			printf("0\n");
			continue;
		}
		if(m == 1)
		{
			printf("%lld\n",qpow(v,2*n-2));
			continue;
		}
		if(n<=12 && v<=2)
		{
			for(int sta = 0;sta < (1<<(n-1+n-1));sta++)
			{
				bool mar=true;
				for(int i = 1;i <= n;i++) x[i] = 0;
				for(int i = 1;i <= m;i++) x[c[i]] = d[i];
				for(int i = 0;i < n-1;i++)
				{
					int a=(sta&(1<<(n-1+n-1-i*2-1))),b=(sta&(1<<(n-1+n-1-i*2-2)));
					if(a==0) a=1;
					else a=2;
					if(b==0) b=1;
					else b=2;
					if(x[i+1] == a)
					{
						if(x[i+2]>0 && x[i+2]!=b)
						{
							mar = false;
							break;
						}
						if(!x[i+2]) x[i+2] = b;
					}
				}
				if(mar) ans++;
			}
			printf("%lld\n",ans);
			continue;
		}
	}
	
}
/*
1
817800451 1 578482625
*/
