#include <bits/stdc++.h>
#define ll long long
#define lll __int128

using namespace std;

namespace Solve
{
	const ll mod = 1e9 + 7;
	const int N = 2e3; 

	set<int> chk[N + 5];	
	ll f[N + 5][N + 5];
	int a[N + 5];
	
	ll addMod(ll a, ll b) {return a + b >= mod ? a + b - mod : a + b;}
	
	void Sol()
	{
		int n, m, v;
		scanf("%d%d%d", &n, &m, &v);
		for (int i = 1; i <= v; ++i) chk[i].clear();
		for (int i = 1; i <= n; ++i) a[i] = -1;
		bool fail = false;
		for (int i = 1; i <= m; ++i)
		{
			int pos, v;
			scanf("%d%d", &pos, &v);
			chk[pos].insert(v);
			if ((int)chk[pos].size() > 1) fail = true;
			a[pos] = v;
		}
		if (fail)
		{
			puts("0");
			return;
		}
		
		memset(f, 0, sizeof f);
		if (a[n] == -1) for (int i = 1; i <= v; ++i) f[n][i] = 1;
		else f[n][a[n]] = 1;
		for (int i = n - 1; i >= 1; --i)
		{
			ll sum = 0;
			for (int j = 1; j <= v; ++j) sum = addMod(f[i + 1][j], sum);
			if (a[i] != -1)
			{
				f[i][a[i]] = 1ll * (v - 1) * v % mod * (i == n - 1 ? 1 : sum) % mod;
				f[i][a[i]] = addMod(f[i][a[i]], sum);
			}
			else
			{
				for (int j = 1; j <= v; ++j)
				{
					f[i][j] = 1ll * (v - 1) * v % mod * (i == n - 1 ? 1 : sum) % mod;
					f[i][j] = addMod(f[i][j], sum);
				}
			}
		}
		ll ans = 0;
		if (a[1] == -1) for (int i = 1; i <= v; ++i) ans = addMod(ans, f[1][i]);
		else ans = f[1][a[1]];
		printf("%lld\n", ans);
	}
}

using Solve::Sol;

int main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout); 
	int t = 1;
	scanf("%d", &t);
	while (t--) Sol();
	return 0;
}
