#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAXN 100005
const int MOD(1e9+7);

int t, n, m, v;
int c[MAXN], d[MAXN];
struct Node{int p, x;}nd[MAXN];

int qpow(int x, int k=MOD-2){int res(1); for (; k; k>>=1, (x*=x)%=MOD) if (k&1) (res*=x)%=MOD; return res;}

namespace sol1{  // A
	bool chk(){if (n != m) return false; for (int i(1); i<=m; ++i) if (i != nd[i].p) return false; return true;}
}

namespace sol2{
	int a[13], b[13], c[13];
	bool chk(){
		memset(c, 0, sizeof(c)); for (int i(1); i<=m; ++i) c[nd[i].p] = nd[i].x;
		for (int i(1); i<n; ++i){
			if (!c[i]) c[i] = a[i] == 1 ? 2 : 1;
			else{
				if (c[i] == a[i] && c[i+1] != b[i] && c[i+1]) return false;
				if (c[i] == a[i]) c[i+1] = b[i];
			}
		}
		return true;
	}
	void sol(){
		int ans(0);
		for (int i(0); i<(1<<(2*(n-1))); ++i){
			for (int j(1); j<n; ++j) a[j] = (i>>(2*j-2)&1)+1, b[j] = (i>>(2*j-1)&1)+1; ans += chk();
		}
		cout << ans << '\n';
	}
}

signed main(){
	freopen("assign.in", "r", stdin); freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	for (cin >> t; t; --t){
		cin >> n >> m >> v;
		for (int i(1); i<=m; ++i) cin >> nd[i].p >> nd[i].x; sort(nd+1, nd+m+1, [](Node a, Node b){return a.p < b.p;}); bool fl(0);
		for (int i(1); i<m; ++i) if (nd[i].p == nd[i+1].p && nd[i].x != nd[i+1].x){cout << "0\n"; fl = 1; break;} if (fl) continue;
		if (m == 1) cout << qpow(v*v%MOD, n-1) << '\n'; else if (sol1::chk()) cout << qpow((v*(v-1)+1)%MOD, n-1) << '\n';
		else if (n <= 12 && m <= 12 && v == 2) sol2::sol(); else cout << "1\n";
	}
	
	return 0;
}

