#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 10;
const ll mod = 1e9 + 7;
int t,m,v,c,d;
ll n; 
map <ll,ll> MAP; 
bool check();
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&t);
	while (t--)
	{
		ll ans = 1;
		scanf("%lld%d%d",&n,&m,&v);
		bool flag = 0,kf = 0;
		MAP.clear();
		for (int i = 1;i <= m;i++)
		{
			scanf("%d%d",&c,&d);
			if (MAP[c] != 0 and MAP[c] != d)
				flag = 1;
			if (d != 1)
				kf = 1;
			MAP[c] = d;
		}
		if (check())continue;
		if (flag){printf("0\n");continue;}
		if (!kf)
		for (int i = 1;i < n;i++)
		{
			if (MAP[i] >= 1 and MAP[i] <= v and MAP[i + 1] >= 1 and MAP[i + 1] <= v)
				ans = (ans * (v * v - v + 1)) % mod;
			else
				ans = (ans * (v * v)) % mod;//cout << MAP[i] << ' ' << MAP[i + 1] << endl;
		}cout << endl;
		printf("%d\n",ans);
	}
	return 0;
}
bool check()
{
	int ans = 1;
	if (n == m)
	{
		for (int i = 1;i <= m;i++)
			ans = (ans * (v - 1)) % mod;
		cout << ans << endl;
		return 1;
	}
	return 0;
}
