#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+7,mod=1e9+7;
struct node{
	ll x,val;
}a[N],b[N];
ll c[N],f[N];
ll n,m,k,q,t,cnt,tmp,sum,pos,ans,mx=-1e9,x,y,z,w,l,r,mid,fl,v;
ll rd(){
	int f=1,x=0;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return x*f;
}
ll qpow(ll a,ll b){
	ll ans=1;//cout<<a<<' '<<b<<"\n";
	while(b){
		if(b&1)ans=ans*a%mod;
		a=a*a%mod,b>>=1;
	}
	return ans;
}
bool cmp(node a,node b){
	return a.x<b.x;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
//  n==1ʱ 
	q=rd();
	while(q--){
		n=rd(),m=rd(),v=rd(),fl=1,f[0]=1;
		for(int i=1;i<=m;i++)a[i].x=rd(),a[i].val=rd();
		sort(a+1,a+1+m,cmp);
	//	cout<<n<<' '<<m<<" "<<k<<"\n";
//		if(m==1){
//			printf("%lld\n",qpow(v,(n-1)*2));continue;
//		}
	//	a[m+1]={n,1},
		pos=0;
		for(int i=1;i<=m&&fl;i++){
			if(a[i].x==a[i-1].x&&a[i].val!=a[i-1].val)fl=0;
			else if(a[i].x!=a[i-1].x)b[++pos]=a[i];
		}
		if(!fl||n==1){
			printf("0\n");continue;
		}
		m=pos;
	//	for(int i=1;i<=m;i++)cout<<b[i].x<<" ";cout<<"\n";
		f[1]=qpow(v,2*(b[1].x-1));
		for(int i=2;i<=m&&fl;i++){
			ll tmp=b[i].x-b[i-1].x;//cout<<b[i].x<<" "<<b[i-1].x<<" "<<tmp<<"\n";
			f[i]=f[i-1]*((qpow(v,2*tmp)-qpow(v,tmp)+qpow(v,tmp-1))%mod)%mod;
		}
		//cout<<"gds\n";
		if(b[m].x==n)printf("%lld\n",f[m]);
		else printf("%lld\n",f[m]*qpow(v,2*(n-b[m].x))%mod);
	}
	return 0;
}
