#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define fo(i,a,b) for(int i=(a);i<=(b);i++)
#define de(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
const ll N=2e6+50,M=+50,L=0;
const ll inf=1e9,P=1e9+7;
ll read(){
	ll x=0,f=0;char c=getchar();
	while(c<'0'||c>'9') f|=(c=='-'),c=getchar();
	while('0'<=c&&c<='9') x=x*10+c-'0',c=getchar();
	return f?-x:x;
}
int T,n,m;
ll v;
int pos[N];
ll pw[N];
unordered_map<int,int> mp;
ll qpow(ll a,ll b){
	ll res=1;
	for(;b;b>>=1,a=a*a%P) if(b&1) res=res*a%P;
	return res;
}
ll calc(int d){
	ll ans=0;
	fo(i,0,d-1) (ans+=v*(v-1)%P*pw[2*d-i]%P)%=P;
	(ans+=(v*v-v+1)%P*pw[d])%=P;
	return ans;
}
void work(){
	n=read(),m=read(),v=read();
	pos[m+1]=0;
	fo(i,1,m){
		pos[i]=read();
		int w=read();
		if(mp.count(pos[i])&&mp[pos[i]]!=w){printf("0\n");return ;}
		mp[pos[i]]=w;
	}
	if(m==1){
		printf("%lld\n",qpow(v*v%P,n-1));
		return ;
	}
	pw[0]=1;
	fo(i,1,n*2+10) pw[i]=pw[i-1]*v%P;
	sort(pos+1,pos+1+m);
	m=unique(pos+1,pos+1+m)-pos-1;
	ll ans=pw[2*(pos[1]-1)];
	fo(i,1,m){
		if(i!=1&&pos[i-1]+1!=pos[i]){
			ans=ans*calc(pos[i]-pos[i-1]-1)%P;
		}
		if(pos[i]!=n){
			if(pos[i+1]-1==pos[i])
				ans=(v*v-v+1)%P*ans%P;
		}
//		cout<<ans<<" "<<calc(pos[i]-pos[i-1]-1)<<endl;
	}
	ll w=0,d=n-pos[m];
	fo(i,0,d-1) (w+=v*(v-1)%P*pw[2*d-i-2])%=P;
	(w+=pw[d])%=P;
	printf("%lld\n",ans*w%P);
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
//	v=2,cout<<calc(1);
	T=read();
	while(T--){
		mp.clear();
		work();
	}
	return 0;
}
/*
1
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2
*/
