#include<iostream> 
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define ll long long
struct node
{
	ll c, d;
};
const ll mod = 1000000007;
ll T, n, m, v;
node a[100010], okk[100010];
ll cnt;
bool cmp(node x, node y)
{
	if(x.c != y.c)return x.c < y.c;
	else return x.d != y.d;
}
ll qpow(ll a, ll b)
{
	ll ans = 1;
	while(b)
	{
		if(b & 1) ans = (ans * a) % mod;
		a = (a * a) % mod;
		b >>= 1;
	}
	return ans;
}
signed main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	cin >> T;
	while(T--)
	{
		scanf("%lld%lld%lld", &n, &m, &v);
		bool flaga = (n == m);
		for(int i = 1; i <= m; i++)
		{
			scanf("%lld%lld", &a[i].c, &a[i].d);
			if(a[i].c != i)
			{
				flaga = 0;
			}
		}
		if(m <= 1)
		{
			ll ans = qpow((v * v) % mod, n - 1);
			printf("%lld\n", ans);
			continue;
		}
		if(flaga)
		{
			ll ans = 1;
			for(int i = 2; i <= n; i++)
			{
				ans *= (((v - 1) * v) + 1) % mod;
				ans %= mod;
			}
			printf("%lld\n", ans);
			continue;
		}
		sort(a + 1, a + m + 1, cmp);
		bool okk = 1;
		for(int i = 1; i <= m; i++)
		{
			if(a[i].c == a[i - 1].c && a[i].d != a[i - 1].d)
			{
				okk = 0;
				printf("0\n");
				break;
			}
		}
		if(!okk) continue;
		ll ans = qpow((v * v) % mod, a[1].c - 1);
		int lst = 1;
//		cout << '\n';
		for(int i = 2; i <= m; i++)
		{
			if(a[i].c == a[lst].c && a[i].d == a[lst].d)
				continue;
//			cout << a[i].c << ' ' << a[lst].c << '\n';
			ans = (ans * ((qpow((v * v) % mod, a[i].c - a[lst].c) - (qpow(v, a[i].c - a[lst].c - 1) * (v - 1)) % mod + mod) % mod)) % mod;
			ans = (ans + mod) % mod;
			lst = i;
		}
		ans = (ans * qpow((v * v) % mod, n - a[m].c)) % mod;
		printf("%lld\n", ans);
		
		
	}
	return 0;
}
