#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=1e5+50;
ll n,m,v;
inline ll qp(ll x,ll y){
	ll res=1;
	while(y)res=res*(y&1?x:1)%mod,x=x*x%mod,y>>=1;
	return res;
}
struct data{
	ll x,v;
}a[N];
bool cmp(data x,data y){
	return x.x<y.x;
}
void solve(){
	cin>>n>>m>>v;
	for(int i=1;i<=m;i++)cin>>a[i].x>>a[i].v;
	sort(a+1,a+1+m,cmp);
	for(int i=1;i<m;i++){
		if(a[i].x==a[i+1].x&&a[i].v!=a[i+1].v){
			cout<<"0\n";
			return ;
		}
	}
	ll res=1;
	res=res*qp(v,2*(a[1].x-1))%mod;
	for(int i=1;i<m;i++){
		ll tmp,tmp1,tmp2;
		if(a[i].x==a[i+1].x&&a[i].v==a[i+1].v)continue;
		tmp1=qp(v,2*(a[i+1].x-a[i].x));
		tmp2=qp(v,a[i+1].x-a[i].x-1)*(v-1)%mod;
		tmp=(tmp1+mod-tmp2)%mod;
		res=res*tmp%mod;
	}
	res=res*qp(v,2*(n-a[m].x))%mod;
	cout<<res<<"\n";
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	cin>>T;
	while(T--){
		solve();
	}
	return 0; 
} 
