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

const int maxm = 1e5 + 6;
const int mod = 1e9 + 7;

struct node{
	int id, val;
};

int T, n, m, v, tmp1, tmp2, ans, cnt, num_smaller;
int lim_plc[maxm];
bool no_answer;
node lim_val[maxm];

bool cmp(node a, node b){
	if(a.id == b.id){
		return a.val < b.val;
	}
	return a.id < b.id;
}

int qpow(int b, int k){
	int sum = 1;
	while(k){ 
		if(k & 1){
			sum = (sum * b) % mod;
		}
		b = (b * b) % mod;
		k = k / 2;
	}
	return sum % mod;
}

void solve(){
	no_answer = false;
	cnt = 0, num_smaller = 0;
	cin >> n >> m >> v;
	for(int i = 1; i <= m; i++){
		cin >> lim_val[i].id >> lim_val[i].val;
	}
	if(m >= 1){
		sort(lim_val + 1, lim_val + m + 1, cmp);
		cnt++;
		lim_plc[cnt] = lim_val[cnt].id;
	}
	for(int i = 2; i <= m; i++){
		if(lim_val[i].id == lim_val[i - 1].id && lim_val[i - 1].val != lim_val[i].val){
			no_answer = true;
			break;
		}
		if(lim_val[i].id != lim_val[i - 1].id){
			cnt++;
			lim_plc[cnt] = lim_val[i].id;
		}
	}
	if(no_answer){
		cout << "0" << endl;
		return;
	}
	for(int i = 2; i <= cnt; i++){
		if(lim_plc[i] == lim_plc[i - 1] + 1){
			num_smaller++;
		}
	}
	ans = qpow(v * v % mod, n - 1 - num_smaller);
	ans = ans * qpow(v * (v - 1) % mod + 1 % mod, num_smaller) % mod;
	cout << ans << endl;
	return;
}

signed main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	cin >> T;
	while(T--){
		solve();
	}
	return 0;
}
