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

#define int long long
const int N = 5e5 + 5, mod = 1e9 + 7;
int T, n, m, v, q, ans;
struct node{int pos, val;} a[N], b[N];

inline int qstp(int a, int k) {int res = 1; for(; k; a = a * a % mod, k >>= 1) if(k & 1) res = res * a % mod; return res;}
signed main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	cin >> T;
	for(int step = 1; step <= T; ++step){
		scanf("%lld%lld%lld", &n, &m, &v);
		for(int i = 1; i <= m; ++i) scanf("%lld%lld", &a[i].pos, &a[i].val);
		sort(a + 1, a + 1 + m, [](node A, node B){return A.pos < B.pos;});
		q = 0;
		ans = qstp(v, 2 * (a[1].pos + n - a[m].pos - 1));
		for(int i = 1; i <= m; ++i){
			if(a[i].pos == a[i - 1].pos && a[i].val != a[i - 1].val){
				ans = 0;
				break;
			}
			if(a[i].pos != a[i - 1].pos) b[++q] = a[i];
		}
		if(ans == 0) {puts("0"); continue;}
		for(int i = 1; i < q; ++i){ 
			int res = qstp(v, 2 * (b[i + 1].pos - b[i].pos));
			res = (res - qstp(v, b[i + 1].pos - b[i].pos - 1) * (v - 1) % mod + mod) % mod;
			ans = ans * res % mod; 
		}
		printf("%lld\n", ans);
	}
	return 0;
}
