#include<bits/stdc++.h>
#define N 100100
#define ll long long
#define P 1000000007

using namespace std;

struct DT{
	ll id, v;
}a[N];

bool flag;
ll n, m, v, T, ans, V[20], p[20], k[20];

void init() {
	flag = true;
	ans = 0;
	memset(V, 0, sizeof(V));
	memset(p, 0, sizeof(p));
	memset(k, 0, sizeof(k));
}

bool cmp(const DT&k, const DT&l) {
	if(k.id == l.id) return k.v < l.v;
	return (k.id < l.id);
}

ll ksm(ll a, ll b) {
	ll ans = 1;
	while(b) {
		if(b & 1) ans = ans * a % P;
		a = a * a % P;
		b >>= 1;
	}
	return ans;
}

int dfss(int x) {
	if(x > n) {
		for(int i = 1; i <= n; i ++)
			if(p[i] == V[i] && k[i] != V[i + 1]) return 0;
		return 1;
	}
	if(V[x] != 0) return dfss(x + 1);
	else {
		for(int i = 1; i <= v; i ++) {
			V[x] = i;
			if(dfss(x + 1)) { 
				V[x] = 0;
				return 1;
			} else V[x] = 0;
		}
		return 0;
	}
} 

void dfs(int x) {
	if(x == n) {
		if(dfss(1)) {
			ans ++;
		}
		return;
	}
	for(int i = 1; i <= v; i ++)
		for(int j = 1; j <= v; j ++) {
			p[x] = i, k[x] = j;
			dfs(x + 1);
		}
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%lld", &T);
	while(T --) {
		init();
		
		scanf("%lld %lld %lld", &n, &m, &v);
		
		for(int i = 1; i <= m; i ++)
			scanf("%lld %lld", &a[i].id, &a[i].v);
		sort(a + 1, a + 1 + m, cmp);
		for(int i = 1; i <= m; i ++) {
			if(a[i].id == a[i - 1].id && a[i].v != a[i - 1].v) {
				flag = false;
				break;
			}
		}
		if(flag == false) {
			printf("0\n");
			continue;
		}
		for(int i = 1; i <= m; i ++)
			if(a[i].id != i) {
				flag = false;
				break;
			}
		
		if(n <= 12 && m <= 12) {
			for(int i = 1; i <= m; i ++)
				V[a[i].id] = a[i].v;
			dfs(1);
			printf("%lld\n", ans);
		} else if(m == 1) {
			ans = ksm(v * v % P, n - 1);
			printf("%lld\n", ans);
		} else if(flag == true) {
			ans = ksm((v - 1) * v % P + 1, m - 1);
			if(n - 1 > m - 1)
				ans = ans * ksm(v * v % P, n - m) % P;
			printf("%lld\n", ans);
		} else {
			ans = ksm(v * v % P, a[1].id - 1);
			for(int i = 1; i < m; i ++) {
				if(a[i].id + 1 == a[i + 1].id)
					ans = ans * ((v - 1) * v % P + 1) % P;
				else ans = ans * ((((v - 1) * v % P) * ksm(v * v % P, a[i + 1].id - a[i].id - 1) % P) + ksm(v, a[i + 1].id - a[i].id - 1) % P + v * (v - 1) % P * v % P* ksm(v, a[i + 1].id - a[i].id - 2) % P) % P;
			}
			ans = ans * ksm(v * v % P, n - a[m].id) % P;
			printf("%lld\n", ans);
		}
	}
}
