#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr ll mo = 1000000007;
int n, m, v, dp[100005];
int qpw(int x, int y)
{
	if(!y)
	{
		return 1;
	}
	int tmp = qpw(x, y / 2);
	return 1ll * tmp * tmp % mo * (y % 2 ? x : 1) % mo;
}
struct Node {
	int c, d;
	void in()
	{
		cin >> c >> d;
	}
	Node() {}
	Node(int C, int D) {c = C, d = D;}
} a[100005];
void solve()
{
	cin >> n >> m >> v;
	for(int i=1;i<=m;i++)
	{
		a[i].in();
	}
	map<int, int> mp;
	for(int i=1;i<=m;i++)
	{
		if(mp[a[i].c] && mp[a[i].c] != a[i].d)
		{
			cout << 0 << endl;
			return;
		}
		mp[a[i].c] = a[i].d;
	}
	int cur = 0;
	for(auto i : mp)
	{
		a[++cur] = Node(i.first, i.second);
	}
	memset(dp, 0, sizeof dp);
	dp[1] = qpw(v, 2 * (a[1].c - 1));
	for(int i=2;i<=cur;i++)
	{
		dp[i] = 1ll * dp[i - 1] * (qpw(v, 2 * (a[i].c - a[i - 1].c)) - 1ll * (v - 1) * qpw(v, a[i].c - a[i - 1].c - 1) % mo + mo) % mo;
	}
	cout << 1ll * dp[cur] * qpw(v, 2 * (n - a[cur].c)) % mo << endl;
}
int main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int tt;
	cin >> tt;
	while(tt--)
	{
		solve();
	}
	return 0;
}
