#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
const int mod = 1e9 + 7;
map <int,int> mp;
vector <int> vec;
inline long long fast_pow(long long a,long long b)
{
	if (b <= 0) return 1ll;
	a %= mod;
	long long ans = 1;
	for (; b > 0; b >>= 1,a *= a,a %= mod)
	{
		if (b & 1) ans *= a,ans %= mod;
	}
	return ans;
}
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,n,m,lst;
	long long w,v,v2,ans;
	bool no;
	cin >> T;
	while (T--)
	{
		cin >> n >> m >> v;
		ans = 1;
		w = (1ll * (v - 1) * v % mod + 1) % mod,v2 = 1ll * v * v % mod;
		no = false;
		vec.clear(),mp.clear();
		for (int i = 1,x,y; i <= m; i++)
		{
			cin >> x >> y;
			if (mp[x] && mp[x] != y) no = true;
			if (!mp[x]) vec.push_back(x);
			mp[x] = y;
		}
		if (no)
		{
			cout << "0\n";
			continue;
		}
		sort(vec.begin(),vec.end());
		ans *= fast_pow(v2,vec[0] - 1),ans %= mod;
		lst = vec[0];
		for (int k = 1,i; k < vec.size(); k++)
		{
			i = vec[k];
			if (i == lst + 1) ans *= w;
			else ans *= ((fast_pow(v2,i - lst) - fast_pow(v,i - lst - 1)) % mod + mod) % mod;
			ans %= mod;
			lst = i;
		}
		ans *= fast_pow(v2,n - lst),ans %= mod;
		cout << ans << '\n';
	}
	return 0;
}
/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2

1
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2
*/
