#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
static char buf[1000000],*p1=buf,*p2=buf;
#define GC()	(p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++)
int read(){
	int x = 0; char c = GC();
	while(c < '0' || c > '9') c = GC();
	while(c >= '0' && c <= '9'){
		x = (x << 3) + (x << 1) + c - '0'; c = GC();
	}
	return x;
}
const int mod = 1e9+7, B = 45000;
int n, m, k, V, ans, pw[50010], pw2[50010];
struct A{
	int id, x;
} a[100010], b[100010];
int qpw(int x){ return (ll)pw2[x / B] * pw[x % B] % mod; }
void solve(){
	ans = 1; k = 0;
	int i;
	n = read(); m = read(); V = read();
	for(i = 1; i <= m; i++){
		a[i].id = read(); a[i].x = read();
	}
	sort(a + 1, a + m + 1, [&](const A u, const A v){ return u.id < v.id; });
	for(i = 1; i < m; i++){
		if(a[i].id == a[i + 1].id && a[i].x != a[i + 1].x){ printf("0\n"); return; }
	}
	for(i = 1; i <= m; i++){
		if(i == 1 || a[i].id > a[i - 1].id) b[++k] = a[i];
	}
	for(i = 1, pw[0] = 1; i <= B; i++) pw[i] = (ll)pw[i - 1] * V % mod;
	for(i = 1, pw2[0] = 1; i <= B; i++) pw2[i] = (ll)pw2[i - 1] * pw[B] % mod;
	ans = (ll)ans * qpw(b[1].id - 1 << 1) % mod * qpw(n - b[k].id << 1) % mod;
	for(i = 1; i < k; i++){
		ans = ans * (qpw(b[i + 1].id - b[i].id << 1) + mod - qpw(b[i + 1].id - b[i].id - 1) * (V - 1ll) % mod) % mod;
	}
	printf("%d\n", ans);
}
int main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int t = read();
	while(t--) solve();
	return 0;
}
