#include <iostream>
#include <cstdio>
#include <string.h>
#include <map>
using namespace std;

long long n,m,v,T,a[100005],b[100005],z[100005],dz=1000000007,jg=1,ls;

long long ks(long long l,long long p){
	if(p==1) return l;
	long long lz=ks(l,p/2);
	if(p%2==1) return (lz*lz*l)%dz;
	else return (lz*lz)%dz;
}

int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>T;
	while(T--){
		memset(z,0,sizeof(z));
		jg=1;ls=0;
		cin>>n>>m>>v;
		bool fl=0;
		for(int i=1;i<=m;i++){
			cin>>a[i]>>b[i];
			if(z[a[i]]!=0 && z[a[i]]!=b[i]) fl=1;
			z[a[i]]=b[i];
		}
		if(fl==1 || n==1){
			cout<<0<<endl;
			continue;
		}
		if(n==m){
			ls=(((v*(v-1))%dz)+1)%dz;
			cout<<ks(ls,n-1)<<endl;
			continue;
		}
		if(m==1){
			ls=((v*v)%dz);
			cout<<ks(ls,n-1)<<endl;
			continue;
		}
	}
//	fclose(stdin);fclose(stdout);
	return 0;
}
