#include<bits/stdc++.h>
using namespace std;
#define N 100050
#define mod 1000000007
#define LL long long
LL T, n, m, i, j, k, s, qy, v, a1, a2, b1, b2, a[N], b[N], p[N], ans;
bool cmp(LL x, LL y){
	return a[x] < a[y];
}
LL cf(LL a, LL b) {
	if (!b) return 1;
	else if (b&1) return a*cf(a, b-1)%mod;
	else return cf(a*a%mod, b/2);
}
int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%lld", &T);
	while (T--) {
		scanf("%lld%lld%lld", &n, &m, &v);
		for (i=1; i<=m; i++) scanf("%lld%lld", &a[i], &b[i]);
		for (i=1; i<=m; i++) p[i] = i;
		sort(p+1, p+m+1, cmp);
		ans = cf(v, n+n-2);//所有情况 + 赋初值 
		for (qy=1, s=n-1, i=1; i<m; i++) {
			a1 = a[p[i]], a2 = a[p[i+1]]; 
			if (a1 == a2) {
				if (b[p[i]] != b[p[i+1]]) {
					ans = 0;
					break;
				}
				else continue;
			}
			s -= a2 - a1;//剩余可随便填数量 
			ans -= cf(v, a2-a1-1+s+s)*(v-1)%mod*qy%mod;
			ans += mod, ans %= mod;
			qy *= (cf(v, 2ll*(a2-a1))-cf(v, a2-a1-1)*(v-1)%mod+mod)%mod, qy %= mod;
		}
		printf("%lld\n", ans);
	}
	return 0;
}
