#include <bits/stdc++.h>
#define int ll
#define fi first
//#define endl '\n'
#define il inline
#define se second
#define pb push_back
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
int qpow(int a, int p)
{
	int res = 1ll;
	while (p)
	{
		if (p & 1ll) res = res * a % mod;
		p >>= 1ll;
		a = a * a % mod;
	}
	return res;
}
int n, m, v, ans;
pii a[N];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int T;
	cin >> T;
	while (T--)
	{
		cin >> n >> m >> v, ans = 1;
		for (int i = 1; i <= m; i++)
			cin >> a[i].fi >> a[i].se;
		sort(a + 1, a + m + 1);
		for (int i = 2; i <= m; i++)
		{
			int x = a[i].fi, y = a[i - 1].fi;
			if (x == y && a[i].se != a[i - 1].se)
			{
				ans = 0;
				break;
			}
			if (x != y)
				(ans *= (qpow(v, 2ll * (x - y)) - qpow(v, x - y - 1) * (v - 1) % mod + mod) % mod) %= mod;
		}
		(ans *= qpow(v, 2ll * (a[1].fi - 1 + n - a[m].fi))) %= mod;
		cout << ans << endl;
	}
	return 0;
}
