#include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
long long t,n,m,v,c,d;
bool flag;
map<long long,long long>val;
long long qpow(long long x,long long y)
{
	long long a=x,res=1;
	while(y>0)
	{
		if(y&1)res=res*a%mod;
		a=a*a%mod;
		y>>=1;
	}
	return res;
}
int dfs(int x,int a,int b)
{
	if(x==n-1)
	{
		if(val[x]==a and val[x+1]!=b)return 0;
		return 1;
	}
	if(val[x]==a and val[x+1]!=b)return 0;
	return dfs(x+1,1,2)+dfs(x+1,1,1)+dfs(x+1,2,1)+dfs(x+1,2,2);
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	while(t--)
	{
		val.clear();
		cin>>n>>m>>v;
		flag=1;
		for(int i=1;i<=m;i++)
		{
			cin>>c>>d;
			if(val.count(c) and val[c]!=d)
				flag=0;
			val[c]=d;
		}
		if(flag==0)
		{
			cout<<0<<endl;
			continue;
		}
		if(m<=1)
		{
			cout<<qpow(v,2*(n-1))<<endl;
			continue;
		}
		if(n==m and n>500)
		{
			cout<<qpow(v-1,2*(n-1))<<endl;
			continue;
		}
		cout<<dfs(1,1,2)+dfs(1,1,1)+dfs(1,2,1)+dfs(1,2,2)<<endl;
	}
	return 0;
}
