#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <map>
#define int long long
using namespace std;

map<int, int> mp;
const int mod = 1e9+7;

int qpow(int a, int b)
{
	int base = a;
	int ans = 1;
	while(b)
	{
		if(b & 1);
		{
			ans *= base;
			ans %= mod;
		}
		base *= base;
		base %= mod;
		b /= 2;
	}
	return ans;
}

signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	cin >> T;
	while(T--)
	{
		mp.clear();
		int n, m, v;
		cin >> n >> m >> v;
		bool flag = 1;
		for(int i = 1; i <= m; i++)
		{
			int x, y;
			cin >> x >> y;
			if(mp[x] != y && mp[x] != 0)flag = 0;
			mp[x] = y;
		}
		if(!flag)
		{
			cout << 0 << endl;
			continue;
		}
		if(m == 1)
		{
			cout << qpow((v * v) % mod, n - 1) << endl;
			continue;
		}
		if(n == m)
		{
			cout << qpow((v * v - v + 1) % mod, n - 1) << endl;
			continue;
		}
		int ans = 1;
		for(int i = 1; i < n; i++)
		{
			int ths = v * v;
			if(mp[i + 1] && mp[i]) ths = ths - v + 1;
			ans = (ans * ths) % mod;
		}
		cout << ans << endl;
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2
*/
