#include <iostream>
#define int long long
#define endl '\n' 
constexpr int MOD=1e9+7;
constexpr int maxn=100010;
using namespace std;
int n, m, v, t, ans=0, var[maxn];
int fpow(int a, int b){
	if (b==0) return 1;
	if (b==1) return a;
	int t=fpow(a, b>>1);
	if (b&1) return (t*t%MOD)*a%MOD;
	else return (t*t)%MOD;
}
signed main(void){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	while (t--){
		bool flag=false;
		cin>>n>>m>>v;
		for (int i=1, c, d; i<=m; i++){
			cin>>c>>d;
			if (var[c]!=d&&var[c]) flag=true;
			else var[c]=d;
		}
		if (flag){
			cout<<0<<endl;
			continue;
		}
		int ans=1;
		ans=fpow((v*v)%MOD, n-1);
		cout<<ans<<endl;
	}
} 
