#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
const int maxm=1e5+10;
int n,m,v;

long long pas=1;
long long fpow(long long p,long long q){
	long long ret=1;
	if(q==0)return 1;
	while(q){
		if(q&1)ret=ret*p%mod;
		q>>=1;
		p=p*p%mod;
	}
	return (ret%mod+mod)%mod;
}
void solve(){
	cin >> n >> m >> v;
	map<int,int> x;
	bool end=false;
	for(int i=1;i<=m;i++){
		int t1,t2;
		cin >> t1 >> t2;
		if(t1>n)continue;
		if(x[t1]!=0&&x[t1]!=t2&&!end){
			cout<<0<<endl;
			end=true;
		}
		x[t1]=t2;
	}
	if(end)return;
	m=x.size();
	pas=fpow((v-1)*v%mod+1%mod,m-1)*fpow(v*v%mod,n-m)%mod;
	cout<<pas<<endl;
	return;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t;
	cin >> t;
//	t=4;
	while(t--){
		solve();
	}
	return 0;
}
