//T2(100)
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 100010, mod = 1e9 + 7;
ll fpow(ll x, ll k){
	if (!k)
		return 1;
	ll t = fpow(x, k >> 1);
	if (k & 1)
		return t * t % mod * x % mod;
	return t * t % mod;
}
ll mmi(ll x){
	return fpow(x, mod - 2);
}

struct A{
	int c, d;
} a[N];
int T, n, m;
ll V, inv;

ll S(ll k){//V的0到k次方和 
	return (fpow(V, k + 1) - 1 + mod) % mod * inv % mod;
}
void solve(){
	scanf("%d %d %lld", &n, &m, &V), inv = mmi(V - 1);
	for (int i = 1; i <= m; i++)
		scanf("%d %d", &a[i].c, &a[i].d);
	sort(a + 1, a + m + 1, [](A x, A y){return x.c != y.c ? x.c < y.c : x.d < y.d;});
	for (int i = 1; i < m; i++)
		if (a[i].c == a[i + 1].c && a[i].d != a[i + 1].d){
			printf("0\n");
			return ;
		}
	a[m + 1].c = n;
	//初值 
	ll val = fpow(V, 2 * (a[1].c - 1));
	//dp
	for (int i = 1; i <= m && a[i].c < n; i++){
		int len = a[i + 1].c - a[i].c;
		if (!len)
			continue; 
		ll coaf = (V - 1) * V % mod * fpow(V, 2 * (len - 1)) % mod;
		if (len > 1)
			(coaf += (S(2 * len - 2) - S(len - 1) + mod) % mod * (V - 1) % mod) %= mod;
		(coaf += fpow(V, len - (i != m))) %= mod;
		val = val * coaf % mod;
	}
	//答案 
	printf("%lld\n", val);
}

int main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	
	scanf("%d", &T);
	for (int it = 1; it <= T; it++)
		solve();
	return 0;
}
