#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int N = 1e5 + 10, MOD = 1e9 + 7;
struct str{
	int c, d;
}s[N];
int n, m, v;
long long f[N][3];
long long qpow(long long x, int k){
	long long ret = 1;
	while(k){
		if(k & 1)
			ret = ret * x % MOD;
		x = x * x % MOD, k >>= 1;
	}
	return ret;
}
void solve(){
	long long temp = qpow(v, s[1].c - 1);
	f[1][0] = temp * (temp - 1 + MOD) % MOD, f[1][1] = temp, f[1][2] = 0, s[0].c = 0;
	for(int i = 2; i <= m; i++){
		if(s[i].c == s[i - 1].c){
			if(s[i].d != s[i - 1].d){
				cout << "0\n";
				return;
			}
			else{
				f[i][0] = f[i - 1][0], f[i][1] = f[i - 1][1], f[i][2] = f[i - 1][2];
				continue;
			}
		}
		int k = s[i].c - s[i - 1].c;
		long long t1 = qpow(v, k - 1), t2 = t1 * v % MOD;
		f[i - 1][0] = (f[i - 1][0] + f[i - 1][1]) % MOD;
		f[i][0] = f[i - 1][0] * t2 % MOD * (t2 - 1 + MOD) % MOD;
		f[i][1] = f[i - 1][0] * t1 % MOD, f[i][2] = f[i - 1][0] * t1 % MOD * (v - 1) % MOD;
		f[i][0] += f[i - 1][2] * t2 % MOD * (t2 - 2 + MOD) % MOD;
		f[i][1] += f[i - 1][2] * 2 % MOD * t1 % MOD, f[i][2] = f[i - 1][2] * 2 % MOD * t1 % MOD * (v - 1) % MOD;
		f[i][0] %= MOD, f[i][1] %= MOD, f[i][2] %= MOD;
	}
	cout << (f[m][0] + f[m][1] + f[m][2]) % MOD  * qpow(v, 2 * (n - s[m].c)) % MOD << "\n";
}
int main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int Cases;
	cin >> Cases;
	while(Cases--){
		cin >> n >> m >> v;
		for(int i = 1; i <= m; i++)
			cin >> s[i].c >> s[i].d;
		sort(s + 1, s + m + 1, [](const str &x, const str &y){return x.c < y.c;});
		solve();
	}
} 
