#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int T,m,cover;
long long n,v;
map<int,int> num;
void init()
{
	cover=0;
	num.clear();
}
long long qk(long long x,long long c)
{
	long long ans=1,cc=x;
	while(c>0)
	{
		if(c%2==1) ans=(ans*cc)%mod;
		cc=(cc*cc)%mod,c>>=1;
	}
	return ans%mod;
}
long long dfs(long long x,int last)
{
	if(x==n) return 1;
	long long ans=0;
	for(register int i=1;i<=v;++i)
	{
		if(i==last&&num[x+1]!=0) ans=(ans+dfs(x+1,i))%mod;
		else ans=(ans+v*dfs(x+1,i))%mod;
	}
	return ans;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--)
	{
		init();
		scanf("%lld%d%lld",&n,&m,&v);
		int t=0;
		for(register int i=1;i<=m;++i)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			if(num[x]!=0&&num[x]!=y) cover=1;
			else if(num[x]==y) t++;
			num[x]=y;
		}
		if(cover==1)  printf("0\n");
		else if(m-t==1) printf("%lld\n",qk(v,2*n-2));
		else printf("%lld\n",dfs(1,0));
	}
	return 0;
}
/*
5
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2
*/
