/*
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
*/

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int mod = 1e9 + 7;
const int N = 1e5 + 10;

ll qpow(ll x, int p) {
	ll res = 1;
	for(; p; p >>= 1) {
		if(p & 1) res = (res * x) % mod;
		x = (x * x) % mod;
	}
	return res;
}

struct node {
	int c, d;
} lim[N];

bool cmp(node x, node y) {
	return x.c < y.c;
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	int t;
	cin >> t;
	while(t--) {
		int n, m, v;
		cin >> n >> m >> v;
		for(int i = 1; i <= m; i++) {
			cin >> lim[i].c >> lim[i].d;
		}
		sort(lim + 1, lim + m + 1, cmp);
		
		bool flag = true;
		
		for(int i = 2; i <= m; i++) {
			if(lim[i].c == lim[i - 1].c && lim[i].d != lim[i - 1].d) {
				flag = false;
				break;
			}
		}
		if(!flag) {
			cout << 0 << endl;
			continue;
		}
		if(v == 1) {
			cout << 1 << endl;
			continue;
		}
		
		ll ans = qpow(v, 2 * (n - 1));
		for(int i = 2; i <= m; i++) {
			if(lim[i].c == lim[i - 1].c) continue;
			ll inv = qpow(qpow(v, 2 * (lim[i].c - lim[i - 1].c)), mod - 2);
			ans = ans - ans * (qpow(v, lim[i].c - lim[i - 1].c - 1) * (v - 1) % mod) % mod * inv % mod;
			ans = (ans + mod) % mod; 
		}
		cout << ans << endl;
	}
	
	return 0;
}
