#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long N = 1e9 + 5,M = 1e5 + 5,V = 1e9 + 5;
long long n,m,v;
int c[M],d[M];
int T;
bool fa = true,fb = true; 
long long ans = 0;
map<long long,long long>mp;
int a[18],b[18];
long long dfs(int x){
	if(x == n){
//		for(int i = 1;i < n;i++){
//			cout << "(" << a[i] << ',' << b[i] << ") ";
//		}
		for(int i = 1;i < n;i++){
			if(a[i] == mp[i] && mp[i + 1] != 0 && mp[i + 1] != b[i]){
				return 0;
			}
		}
		return 1;
	}
	int ans = 0;
	for(int i = 1;i <= v;i++){
		for(int j = 1;j <= v;j++){
			a[x] = i;
			b[x] = j;
			ans = (ans + dfs(x + 1)) % mod;
			a[x] = 0;
			b[x] = 0;
		}
	}
	return ans;
}
long long fastpower(long long n,long long p){
	long long ans = 1;
	while(p){
		if(p & 1){
			ans = ans * n % mod;
		}
		n = n * n % mod;
		p >>= 1;
	}
	return ans;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin >> T;
	while(T--){
		mp.clear();
		bool nop = 1;
		fa = true;
		fb = true;
		cin >> n >> m >> v;
		if(m != n) fa = false;
		for(int i = 1;i <= m;i++){
			
			cin >> c[i] >> d[i];//X_c[i] = d[i]
			if(mp[c[i]] != 0) nop = false;
			mp[c[i]] = d[i];
			if(c[i] != i) fa = false;
			if(d[i] != 1) fb = false;
		}
		if(nop == false){
			cout << 0 << endl;
			continue;
		}
		if(n <= 12 && m <= 12){
			//cout << mp[1] << mp[2] << endl;
			//cout << T << endl;
			cout << dfs(1) << endl;
			continue;
		}
		if(m <= 1){
			int ind = c[1];
			long long ans = fastpower(v,2 * n - 3) * (v - 1) % mod;
			ans = (ans + (fastpower(v,2 * c[1] - 2))% mod * fastpower(v,n - c[1])) % mod;
			cout << ans << endl;
			continue;
		}
//		else{
//			cout << 0 << endl;
//		}
		if(fa == true){
			//cout << "A";
//			long long ans = 1;
//			long long bns = (v % mod * (v - 1) % mod) * (v % mod * (v - 1) % mod) % mod;
//			long long cns = bns;
//			for(int i = 1;i < n;i++){
//				ans = (ans + cns) % mod;
//				cns = (cns * bns) % mod;
//			}
			ans = fastpower((v * (v - 1) % mod + 1),2 * n - 2) % mod;
			cout << ans % mod << endl;
			continue;
		}
		
		cout << 0 << endl;
	}
	return 0; 
}
