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

const int N = 1e5 + 10;

const int mod = 1e9 + 7;

int T, n, m, v;

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

int qpow(int a, int b, int mod){
	int res = 1;
	for (; b; b >>= 1){
		if (b & 1) res = (res * a) % mod;
		a = (a * a) % mod;
	}
	return res;
}

int sol(){
	
	cin >> n >> m >> v;
	
	for (int i = 1; i <= m; i++){
		cin >> lim[i].c >> lim[i].d;
	}
	
	sort(lim + 1, lim + 1 + m, [](node a, node b){
		return a.c < b.c;
	});
	
	for (int i = 1; i <= m; i++){
		if (lim[i].c == lim[i - 1].c)
			if (lim[i].d != lim[i - 1].d)
				return 0;
	}
	
	int res = 1, lst = 0;
	
	for (int i = 1; i <= m; i++){
		
		int c = lim[i].c, d = lim[i].d;
		
		int dis = c - lst; lst = c;

		if (dis == 0) continue;
		
		if (i == 1){
			if (c == 1) continue;
			res = (res * qpow(v, 2 * (c - 1), mod)) % mod;
			continue;
		}
		
		if (dis == 1){
			res = (res * (v * v % mod - (v - 1) + mod) % mod) % mod;
			res = (res + mod) % mod;
		} else{
			int t = (qpow(v, dis + dis, mod) - qpow(v, dis - 1, mod) * (v - 1) % mod + mod) % mod;
			res = (res * t) % mod;
		}
		
		
	}
	
	if (lst == n - 1){
		res = (res * v % mod * v % mod) % mod;
	}
	
	if (lst < n - 1){
		res = (res * qpow(v, 2 * (n - lst), mod)) % mod;
	}
	
	return res % mod;
	
}

signed main(){
 	
 	freopen("assign.in", "r", stdin);
 	freopen("assign.out", "w", stdout);
 	
 	cin.tie(0) -> sync_with_stdio(0);
 	
 	for (cin >> T; T--; ){
 		cout << sol() << endl;
 	}
 	
 	return 0;
 }
