#include<cstdio>
#include<algorithm>
#include<map>
#define int long long
#define p 1000000007
#define N 114514
using namespace std;
int T,n,m,v; 
int c[N],d[N];
map<int,int>mp;
int qpow(int a,int b){
	int res=1;
	while(b){
		if(b&1)res=res*a%p;
		a=a*a%p;
		b>>=1;
	}
	return res;
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&T);
	while(T--){
		mp.clear();
		//printf("%lld\n",T);
		scanf("%lld%lld%lld",&n,&m,&v);
		int flgg=0;
		int cnt=m;
		for(int i=1;i<=m;i++){
			scanf("%lld%lld",&c[i],&d[i]);
			if(mp.find(c[i])!=mp.end()){
				if(mp[c[i]]!=d[i])flgg=1;
				cnt--;
			}
			mp[c[i]]=d[i];
		}
		int flg=1;
		for(int i=1;i<=m;i++)if(c[i]!=i)flg=0;
		if(flgg)puts("0");
		else if(n==m&&flg){
			int bas=(v*(v-1)%p+1)%p;
			printf("%lld\n",qpow(bas,n-1));
		}else if(m==1){
			int bas=v*v%p;
			printf("%lld\n",qpow(bas,n-1));
		}else if(cnt==1){
			int bas=v*v%p;
			printf("%lld\n",qpow(bas,n-1));
		}
	}
}
/*
1
12 2 2
7 2
7 2
*/
