#include<iostream>
#include<map>
using namespace std;
#define int long long
int t,n,m,v;
int mod=1e9+7;
int c[100010],d[100010];
map<int,int> ma;
int qpow(int x,int y) {
	int res=1;
	while(y) {
		if(y%2==1) {
			res*=x;
			res%=mod;
		}
		x=x*x;
		x%=mod;
		y/=2;
	}
	return res;
}
signed main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	while(t--) {
		ma.clear();
		cin>>n>>m>>v;
		int tt;
		int flag=0;
		for(int i=1; i<=m; i++) {
			cin>>c[i]>>d[i];
			if(ma[c[i]]>0){
				flag=1;
				break;
			}
			ma[c[i]]=d[i];
		}
		if(flag==1){
			cout<<0<<endl;
			continue;
		}
		if(n==m) {
			int flag=0;
			cout<<qpow((((v-1)*v)+1),n-1)<<endl;
		}
	}
	return 0;
}
