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

const int mod=1e9+7,N=2e5+10;
int qpow(int nn,int qq,int res=1){
	while(qq){
		if(qq&1)res=1ll*res*nn%mod;
		qq>>=1;
		nn=1ll*nn*nn%mod;
	}return res;
}
struct qp{
	int id,val;
}x[N],xx[N];
int n,m,v;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	int T;
	cin>>T;
	while(T--){
		cin>>n>>m>>v;
		for(int i=1;i<=m;i++)
			cin>>xx[i].id>>xx[i].val;
		sort(xx+1,xx+m+1,[](qp q,qp p){
			return q.id<p.id;
		});
		int cnt=0;
		bool ok=1;
		x[++cnt]=xx[1];
		for(int i=2;i<=m;i++){
			if(xx[i].id==xx[i-1].id)
				if(xx[i].val!=xx[i-1].val){
					ok=0;break;
				}else;
			else x[++cnt]=xx[i];
		}if(!ok){
			cout<<0<<'\n';
			continue;
		}
		m=cnt;
		int ans=1ll*qpow(v,(x[1].id-1)*2)%mod*qpow(v,2*(n-x[m].id))%mod;
		for(int i=2;i<=m;i++){
			ans=1ll*ans*(qpow(v,2*(x[i].id-x[i-1].id))-
			1ll*qpow(v,(x[i].id-x[i-1].id-1))*(v-1)%mod+mod)%mod;
		}cout<<ans<<endl;
	}
	
	return 0;
} 
