#include<bits/stdc++.h>
#define int long long
using namespace std;
const int _=1e5+5,mod=1e9+7;
int n,m,p,ans;
struct da{
	int t,p;
}z[_];
bool cmp(da x,da y){
	return x.t<y.t;
}
int ksm(int x,int k){
	int sum=1;
	while(k){
		if(k&1)
			sum=sum*x%mod;
		x=x*x%mod;
		k>>=1;
	}
	return sum;
}
void add(int l,int r){
	(ans*=(ksm(p,2*(r-l))-ksm(p,r-l-1)*(p-1)%mod+mod))%=mod;
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false),cin.tie(0);
	int T;
	cin>>T;
	while(T--){
		cin>>n>>m>>p,ans=1;
		for(int i=1;i<=m;i++)
			cin>>z[i].t>>z[i].p;
		sort(z+1,z+m+1,cmp);
		for(int i=2;i<=m;i++){
			if(z[i].t==z[i-1].t){
				if(z[i].p!=z[i-1].p)
					ans=0;
				continue;
			}
			add(z[i-1].t,z[i].t);
		}
		(ans*=ksm(p,2*(z[1].t-1)))%=mod,(ans*=ksm(p,2*(n-z[m].t)))%=mod;
		printf("%lld\n",ans);
	}
}
