#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7; 
inline int read(){
	int ans = 0, flg = 1;
	char c = getchar();
	while(c < '0' || c > '9'){
		if(c == '-') flg = -flg;
		c = getchar();
	}
	while(c >= '0' && c <= '9'){
		ans = ans * 10 + c - '0';
		c = getchar();
	}
	return ans * flg;
}
int n, m, v;
bool flagA = true;
int c[100005], d[100005];
int qpow(int a, int b){
	int res = 1;
	while(b){
		if(b & 1){
			res = res * a % mod;
		}
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}
signed main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int T = read();
	while(T--){
		n = read(), m = read(), v = read();
		if(n != m) flagA = false;
		for(int i = 1; i <= m; i++){
			c[i] = read();
			d[i] = read();
			if(c[i] != i) flagA = false;
		}
		if(flagA){
			cout << qpow(v % mod * v % mod - v % mod + 1, n - 1);
			return 0;
		}
	}
	return 0;
}
