#include<bits/stdc++.h>
#define int long long
// #define DEBUG
const int mod = 1e9 + 7;
int n, m, v;
int a[2000000 + 10];
int dp[2010][2010];
int dp2[2000000 + 10][2];
int dp3[2000000 + 10];
int fastpow(int a, int b){
	int res = 1;
	while(b){
		if(b & 1) res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}
void solve(){
	scanf("%lld%lld%lld", &n, &m, &v);
	bool flag = 0;
	if(m == 1){
		int a, b;
		scanf("%lld%lld", &a, &b);
		printf("%lld\n", fastpow(v * v % mod, n - 1));
		return;
	}
	for(int i = 1 ; i <= n ; i++){
		a[i] = 0;
	}
	int cnt = 0, flag2 = 1;;
	for(int i = 1 ; i <= m ; i++){
		int x, y;
		scanf("%lld%lld", &x, &y);
		if(a[x] && a[x] != y)
			flag = 1;
		if(!a[x])
			cnt++;
		if(y != 1)
			flag2 = 0;
		a[x] = y;
	}
	if(flag)
		return puts("0"), void();
	if(cnt == n || flag2){
		for(int i = 1 ; i <= n;  i++)
			dp2[i][0] = dp2[i][1] = dp3[i] = 0;
		dp2[1][0] = 1;
		for(int i = 1 ; i < n ; i++){
			if(a[i]){
				dp2[i + 1][0] = (dp2[i][0] + dp2[i][1]) % mod * (v - 1) % mod * v % mod;
				dp2[i + 1][1] = (dp2[i][0] + dp2[i][1]) % mod;
				dp3[i + 1] = (dp2[i][0] + dp2[i][1]) % mod * (v - 1) % mod;
			}
			else{
				int sum = dp3[i] + dp2[i][1];
				sum %= mod;
				dp2[i + 1][0] = (v * v % mod * dp2[i][0] % mod + v * (v - 1) % mod * sum % mod) % mod;
				dp3[i + 1] = sum * (v - 1) % mod;
			}
		}
		printf("%lld\n", (dp2[n][0] + dp2[n][1]) % mod);
		return;
	}
	for(int i = 1 ; i <= n ; i++)
		for(int j = 0 ; j <= v ; j++)
			dp[i][j] = 0;
	dp[1][0] = 1;
	for(int i = 1 ; i < n ; i++){
		if(a[i]){
			dp[i + 1][0] = (dp[i][0] + dp[i][a[i]]) % mod * (v - 1) % mod * v % mod;
			for(int j = 1 ; j <= v ; j++){
				dp[i + 1][j] = dp[i][0] + dp[i][a[i]];
				dp[i + 1][j] %= mod;
			}
		}
		else{
			int sum = 0;
			for(int j = 1 ; j <= v ; j++)
				sum = (sum + dp[i][j]) % mod;
			dp[i + 1][0] = (v * v % mod * dp[i][0] % mod + v * (v - 1) % mod * sum % mod) % mod;
			for(int j = 1 ; j <= v ; j++)
				dp[i + 1][j] = sum;
		}
	}
	if(a[n])
		printf("%lld\n", (dp[n][a[n]] + dp[n][0]) % mod);
	else{
		int sum = 0;
		for(int j = 0 ; j <= v ; j++)
			sum = (sum + dp[n][j]) % mod;
		printf("%lld\n", sum);
	}
}
signed main(){
	#ifndef DEBUG
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	#endif
	int t;
	scanf("%lld", &t);
	while(t--)
		solve();
	return 0;
}