#include <bits/stdc++.h>
#define N (int)(1e9+3)
#define mod (int)(1e9+7)
#define ll long long
using namespace std;

int T, n, m, v, flag;
bool a[N];
int b[103];
ll ans;

inline void init() {
	flag = 1, ans = 0;
	memset(a, 0, sizeof(bool)*(n+2));
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%d", &T);
	while(T--) {
		scanf("%d%d%d", &n, &m, &v);
		init();
		for(int i=1, x, y; i<=m; i++) {
			scanf("%d%d", &x, &y);
			if(a[x] != 0)
				flag = 100;
			a[x] = true;
			b[x] = y;
			if(x != i && flag == 1)
				flag = 2;
			if(y != 1 && flag == 2)
				flag = 0;
		}
		if(flag == 1) {
			ans = 1;
			for(int i=1; i<n; i++)
				ans = (((ll)v*(v-1)%mod + 1) * ans)%mod;
			printf("%lld\n", ans);
		} else
			puts("0");
	}	
}
