#include <bits/stdc++.h> 
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
int T;
int n, m, v;
int c[N], d[N], ans;
map<int, int> mp;

int my_pow(int x, int k)
{
	if(k == 1) return x;
	if(k == 0) return 1;
	int num = x * x % mod;
	return my_pow(num, k / 2) * my_pow(x, k % 2) % mod;
}

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

