#include<bits/stdc++.h>
#define ll long long
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	return (f?-x:x);
}
const int M=1e5+5,mod=1e9+7;
int T,n,m,v,a[M],b[M],bj[20],f[20],jl[20];
ll ans;
ll ksm(ll x,ll y)
{
	if(!x)return 0;
	ll ji=1;
	while(y)
	{
		if(y&1)ji=ji*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return ji;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	T=read();
	while(T--)
	{
		n=read();m=read();v=read();
		for(int i=1;i<=m;++i)
			a[i]=read(),b[i]=read();
		if(m==1)
		{
			printf("%lld\n",ksm(v,2*(n-m)));
			continue;
		}
		if(n<=12&&v<=2)
		{
			for(int i=1;i<=n;++i)jl[i]=0;
			ans=0;
			for(int i=1;i<=m;++i)
			{
				if(jl[a[i]]&&jl[a[i]]!=b[i])
				{
					ans=-1;
					puts("0");
					break;
				}
				jl[a[i]]=b[i];
			}
			if(ans==-1)continue;
			int len=2*(n-1);
			for(int i=1;i<=len;++i)bj[i]=1;
			for(int tim=0;tim<(1<<len);++tim)
			{
				bj[len]++;
				for(int i=len;i>=1;--i)
					if(bj[i]==3)
					{
						bj[i-1]++;
						bj[i]=1;
					}
					else break;
				for(int i=1;i<=n;++i)jl[i]=-1;
				for(int i=1;i<=m;++i)jl[a[i]]=b[i];
				int flag=0;
				for(int i=1;i<n;++i)
					if(jl[i]==bj[i])
					{
						if(jl[i+1]!=-1)
						{
							if(jl[i+1]!=bj[i+n-1])
							{
								flag=1;
								break;
							}
						}
						else jl[i+1]=bj[i+n-1];
					}
				if(!flag)ans++;
				if(ans>mod)ans-=mod;
			}
			printf("%lld\n",ans);
			continue;
		}
	}
	return 0;
}
/*
3
2 1 2
1 1
*/
