#include <bits/stdc++.h>
using namespace std;

#define int long long

const int mod=1e9+7;
int t,n,m,v;
int c[100005],d[100005];
unordered_map<int,int> M;

inline unsigned int fpow(int a,int b){
	unsigned long long ans=1ull,t=a;
	while(b){
		if(b&1ull) ans=(ans*t)%mod;
		t=(t*t)%mod;
		b>>=1ull;
	}
	return ans;
}

inline void work_A(){
	cout<<fpow(v*(v-1ull)+1ull,n-1ull)<<'\n';
}

inline void work_bf(){
	cout<<fpow(v,2ull*n-2ull)<<'\n';
	return ;
} 

int cc[100005],ccnt=0;
/*
808199 2 954048
529220 1
74581 1
*/
/*
935264382
*/
inline void work_B(){
	memset(cc,0,sizeof cc);
	ccnt=0;
	for(auto v:M){
		cc[++ccnt]=v.first;
	}
	sort(cc+1,cc+1+ccnt);
	unsigned int ans=fpow(v*v,cc[1]-1);
	int res=0ll; 
	for(int i = 2ll; i<=ccnt; i++,res=0ll){
		res=0ll;
		for(int j = 2ll*(cc[i]-cc[i-1]+1ll)-2ll; j>=0ll; j--){
		//	cout<<j<<" \n"[j==0];
			if(j&1ll) res-=fpow(v,j);
			else res+=fpow(v,j);
			res%=mod;
		}
		ans=ans*res%mod;
	}
	ans=ans*fpow(v,2ll*(n-cc[ccnt]))%mod;
	cout<<ans<<'\n';
}

bool flag=true;
bool flagA=true;
bool flagB=true;

signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout); 
	cin>>t;
	while(t--){
		M.clear();
		flag=flagA=flagB=true;
		cin>>n>>m>>v;
		if(m==1){
			int u,v;
			cin>>u>>v;
			work_bf();
			continue;
		}
		for(int i =1; i<=m; i++){
			cin>>c[i]>>d[i];
			if(M[c[i]]&&(M[c[i]]^d[i])){
				puts("0");
				flag=false;
			}
			M[c[i]]=d[i];
			if(c[i]^i) flagA=false;
			if(d[i]^1) flagB=false;
		}
		if(!flag) continue;
		if(flagA) work_A();
		else work_B();
	}	
	return 0;
}
