#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1e9+7;
const int maxn=1e5+10;
int T,n,m,v,dp[maxn],a[maxn],cntNd;
struct Node{
	int c,d;
	Node(int c_,int d_){
		c=c_,d=d_;
	}
	Node(){}
}Nd[maxn];
map<int,int>mp; 
set<int>st;
int ksm(int x,int y){
	int ret=1;
	while(y){
		if(y&1) ret=ret*x%mod;
		x=x*x%mod,y>>=1;
	}
	return ret;
}
bool cmp(Node i,Node j){
	return i.c<j.c;
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdin);
	scanf("%lld",&T);
	while(T--){
		scanf("%lld%lld%lld",&n,&m,&v);
		for(int i=0;i<=m+1;i++) dp[i]=0;
		mp.clear();cntNd=0;
		bool flag=1;
		for(int i=1;i<=m;i++){
			int pos,val;
			scanf("%lld%lld",&pos,&val);
			if(mp.find(pos)==mp.end()){
				mp[pos]=val;
				Nd[++cntNd]=Node(pos,val);
			}
			else if(mp[pos]!=val){
				flag=0;
				break;
			}
		}
		if(!flag){
			printf("0\n");
			continue;
		}
		sort(Nd+1,Nd+1+cntNd,cmp);
		dp[1]=ksm(v,2*(Nd[1].c-1));
		for(int i=2;i<=cntNd;i++){
			int len=Nd[i].c-Nd[i-1].c;
			dp[i]=dp[i-1]*(ksm(v,2*len)-(ksm(v,len-1)*(v-1)%mod)+mod)%mod;
		}
		int ans=dp[cntNd];
		if(Nd[cntNd].c!=n){
			ans=ans*ksm(v,2*(n-Nd[cntNd].c))%mod;
		} 
		printf("%lld\n",ans);
	}
	return 0;
}

