#include<bits/stdc++.h>
using namespace std;
int t,n,m,v;
const int mod=(int)1e9+7;
int fmi(int x,int k){
	if(k==0)return 1;
	if(k==1)return x;
	int ans=fmi(x,k>>1);
	return 1ll*ans*ans%mod*fmi(x,k&1)%mod;
}
void solve(){
	scanf("%d%d%d",&n,&m,&v);
	map<int,int>mp;
	int fl=1;
	while(m--){
		int x,y;scanf("%d%d",&x,&y);
		if(mp[x]!=0&&mp[x]!=y)fl=0;
		else mp[x]=y;
	}
	if(!fl){
		printf("0\n");return;
	}
	int lst=-1,ans=1,sum=n-1,fir;
	for(pair<int,int>x:mp){
		if(lst==-1)fir=x.first;
		else{
			ans=1ll*ans*(fmi(v,(x.first-lst)*2)+mod-1ll*(v-1)*fmi(v,x.first-lst-1)%mod)%mod;
		}
		lst=x.first;
	}
	if(fir>1)ans=1ll*(1ll*ans*fmi(1ll*v*v%mod,fir-1)%mod)%mod;
	if(lst<n)ans=1ll*(1ll*ans*fmi(1ll*v*v%mod,n-lst)%mod)%mod;
	printf("%d\n",ans);
}
int main(){
	freopen("assign.in","r",stdin);freopen("assign.out","w",stdout);
	scanf("%d",&t);
	while(t--)solve();
}
