#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 5, mod =1e9 + 7;

ll t, n, m, v, ans, c[maxn], d[maxn];
map<int, int>mp;
void dfs(int x)
{
	if(x == n) return;
	if(mp[x] && mp[x + 1])
	{
		ans = (ans * 3) % mod;
		dfs(x + 1);
	}
	else if(!mp[x] || !mp[x + 1])
	{
		ans = (ans * 4) % mod;
		dfs(x + 1);
	}
}
int main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> t;
	while(t--)
	{
		cin >> n >> m >> v;
		mp.clear();
		for(int i = 1; i <= m; i++)
		{
			cin >> c[i] >> d[i];
			if(mp[c[i]] && mp[c[i]] != d[i])
			{
				cout << "0\n";
				n = -1;
				break;
			}
			mp[c[i]] = d[i];
		}	
		if(n == -1) continue;
		if(m == n)
		{
			ans = 0;
			ll tmp = 1;
			for(int i = 1; i < n; i++)
			{
				ans = (ans + tmp) % mod;
				tmp = tmp * v * (v - 1) % mod;	
			}
			cout << ans << '\n';
		}
		else if(m == 1) 
		{
			v = v * v % mod;
			ans = 1;
			for(int i = 1; i < n; i++)
			{
				ans = ans * v % mod;
			}
			cout << ans << '\n';
			continue;
		}
		else if(v == 2 && n <= 12 && m <= 12)
		{
			ans = 1;
			dfs(1);
			cout << ans << '\n';
			continue;
		} 
	}
	return 0;
}
