#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
int t, n, m, a[100005], b[100005], c[100005];
long long v;
int tt(int i, int j, int k)
{
	if(j == 0)
		return 0;
	if(i == j)
		return k;
	return 0;
}
long long rr(int ii)
{
	if(ii == n)
	{

		if(a[ii] != 0 && c[ii-1] != 0)
		{
			if(c[ii-1] != a[ii])
				return 0;
		}
		return 1;
	}
	long long s = 0;
	if(b[ii-1] != 0)
	{
		if(a[ii] != 0)
		{
			if(c[ii-1] != a[ii])
				return 0;
			else
			{
				s = (v-1)*v%mod;
				s = (s*rr(ii+1))%mod;
				b[ii] = a[ii];
				for(int i = 1; i <= v; i++)
				{
					c[ii] = i;
					s = (s+rr(ii+1))%mod;
				}
				b[ii] = 0;
			}
		}
		else
		{
			s = (v-1)*v%mod;
			s = (s*rr(ii+1))%mod;
			b[ii] = c[ii-1];
			for(int i = 1; i <= v; i++)
			{
				c[ii] = i;
				s = (s+rr(ii+1))%mod;
			}
			b[ii] = 0;
		}
	}
	else
	{
		if(a[ii] == 0)
		{
			s = v*v;
			s = s%mod;
			s = (s*rr(ii+1))%mod;
		}
		else
		{
			s = (v-1)*v%mod;
			s = (s*rr(ii+1))%mod;
			b[ii] = a[ii];
			for(int i = 1; i <= v; i++)
			{
				c[ii] = i;
				s = (s+rr(ii+1))%mod;
			}
			b[ii] = 0;
		}
	}
	return s;
}
int main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%d", &t);
	while(t--)
	{
		scanf("%d%d%lld", &n, &m, &v);
		memset(a, 0, sizeof(a));
		int ans = 1;
		for(int i = 1, c, d; i <= m; i++)
		{
			scanf("%d%d", &c, &d);
			if(a[c] != 0 && a[c] != d)
				ans = 0;
			a[c] = d;
		}
		if(ans == 1 && v == 1)
			ans = 1;
		else if(ans == 1)
			ans = rr(1);
		printf("%d\n", ans);
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
