#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
#define ll long long
const ll mod = 1e9 + 7; 
ll inline read() {
	ll num = 0, f = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }
	while(ch >= '0' && ch <= '9') { num = (num << 3) + (num << 1) + (ch ^ 48); ch = getchar(); }
	return num * f;
}
int n, m, lim[N], c[N], d[N]; 
ll V, C, f[N][N], s[N]; 
inline void binc(ll &x, const ll &y) { x += y; if(x >= mod) x -= mod; }
inline void bdec(ll &x, const ll &y) { x -= y; if(x < 0) x += mod; }
inline ll inc(ll x, const ll &y) { x += y; if(x >= mod) x -= mod; return x; }
inline ll dec(ll x, const ll &y) { x -= y; if(x < 0) x += mod; return x; }
ll qp(ll a, ll b) {
	ll res = 1; 
	while(b) {
		if(b & 1) res = res * a % mod; 
		a = a * a % mod, b >>= 1;  
	}
	return res; 
}
void solve() {
	n = read(), m = read(), V = read();
	for(int i = 1; i <= n; ++i) lim[i] = 0; 
	for(int i = 1; i <= m; ++i) c[i] = read(), d[i] = read(); 
	for(int i = 1; i <= m; ++i) {
		if(lim[c[i]] && lim[c[i]] != d[i]) return printf("0\n"), void(); 
		lim[c[i]] = d[i]; 
	}
	memset(f, 0, sizeof(f)); 
	f[1][lim[1]] = 1; 
	for(int i = 1; i <= n; ++i) {
		if(lim[i]) {
			for(int j = 1; j <= V; ++j)
				if(j != lim[i]) f[i][j] = 0; 
			binc(f[i][lim[i]], f[i][0]), f[i][0] = 0; 
		}
		for(int j = 1; j <= V; ++j) s[j] = inc(s[j - 1], f[i][j]); 
		for(int j = 1; j <= V; ++j) binc(f[i + 1][j], s[V]); 
		f[i + 1][0] = (f[i + 1][0] + s[V] * V % mod * (V - 1)) % mod; 
		f[i + 1][0] = (f[i + 1][0] + f[i][0] * V % mod * V) % mod; 
	}
	ll ans = 0; 
	for(int j = 0; j <= V; ++j) binc(ans, f[n][j]); 
	printf("%lld\n", ans); 
}
int main() {
#define file
#ifdef file
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
#endif
	int T = read(); 
	while(T--) solve(); 
	return 0;
}
/*
首先如果没有限制的话，那么就令 x[i] = 0 
设 f[i][j] 表示填完前 i 个，然后 x[i] = j 的方案数 

1
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2
*/

