#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10, mod = 1e9 + 7;
typedef long long LL;

int t;
int n, m, v;
int x[N];

int main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	
	scanf("%d", &t);
	
	while (t--)
	{
		LL ans = 1;
		int tmp = 0, st = 0;
		memset(x, 0, sizeof x);
		
		scanf("%d%d%d", &n, &m, &v);
		for (int i = 1; i <= m; i++)
		{
			int a, b;
			scanf("%d%d", &a, &b);
			if (b != 1) st = 1;
			
			if (x[a] == 0) x[a] = b;
			else
			{
				if (x[a] != b) tmp = 1;
			}
		}
		
		if (tmp == 1)
		{
			printf("0\n");
			continue;
		}
		
		if (m == n)
		{
			for (int i = 1; i < n; i++)
				ans = ans * (1LL * v * (v - 1) % mod + 1) % mod;
			printf("%lld\n", ans);
			continue;
		}
		
		if (st == 0)
		{
			for (int i = 1; i < n; i++)
			{
				if (x[i + 1] == 1)
					ans = ans * (1LL * v * (v - 1) % mod + 1) % mod;
				else ans = ans * (1LL * v * v % mod) % mod;
			}
			printf("%lld\n", ans);
			continue;
		}
		
		if (v <= 2)
		{
			for (int i = 1; i < n; i++)
			{
				if (x[i + 1] != 0)
					(ans *= 1LL * 3) %= mod;
				else (ans *= 1LL * 4) %= mod;
			}
			printf("%lld\n", ans);
			continue;
		}
		printf("%lld\n", ans);
	}	
	
	return 0;
}
