#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int M = 1e5 + 5;
map<long long,int>mp;
int turn[M];
long long a[M];
int qp(long long a,long long b)
{
	long long ans = 1;
	while(b)
	{
		if(b & 1)
		{
			ans = (ans * a) % mod;
		}
		a = a * a % mod;
		b >>= 1;
	}
	return ans;
}
int m;
void del()
{
	for(int i = 1;i <= m;i++)
	{
		mp[turn[i]] = 0;
	}
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t;
	cin >> t;
	while(t--)
	{
		int tt;
		m = 0;
		long long n,v;
		cin >> n >>tt >>v;
		long long ans = 1;
		for(int i = 1;i <= tt;i++)
		{
			long long id,x;
			cin >> id >> a[i];
			if(mp[id] > 0 && a[mp[id]] != a[i])
			{
			//	cout << id << " " << x << endl;
				ans = 0;
				continue;
			}
			if(mp[id] == 0)turn[++m] = id;
			mp[id] = i;
		}
		if(ans == 0)
		{
			del();
			cout << 0 << endl;
			continue;
		}
		sort(turn + 1,turn + m + 1);
		if(turn[1] != 1)
		{
//			ans *= ((v - 1) *v % mod + 1);
//			ans %= mod;
			ans *= qp(v * v % mod,max(0,turn[1] - 1));
			ans %= mod;
		}
		for(int i = 1;i < m;i++)
		{
			if(turn[i + 1] == turn[i] + 1)
			{
				ans *= ((v - 1) * v % mod + 1);
				ans %= mod;
			}
			else
			{
				ans *= ((v - 1) *v % mod + 1);
				ans %= mod;
				ans *= qp(v * v % mod,max(0,turn[i + 1] - turn[i]-1));
				ans %= mod;
			}
		}
		if(turn[m] != n)
		{
			ans *= qp(v *v % mod,(n - turn[m]));
			ans %= mod;
		}
		
		cout << ans << endl;
		del();
	}
	return 0;
}
