#include<bits/stdc++.h>
using namespace std;
const int N=100005,mod=1e9+7;
int n,m,V,T,cnt;
struct node{
	int x,val;
}a[N],b[N];
int ksm(int x,int y){
	int s=1;
	while (y){
		if (y&1)s=1ll*s*x%mod;
		x=1ll*x*x%mod,y>>=1;
	}
	return s;
}
void solve(){
	cnt=0;
	cin>>n>>m>>V;
	for (int i=1;i<=m;++i)cin>>a[i].x>>a[i].val;
	sort(a+1,a+m+1,[&](node x,node y){return x.x<y.x;});
	int ans=1;
	for (int i=1;i<=m;++i){
		if (a[i].x==a[i-1].x&&a[i].val!=a[i-1].val){
			ans=0;
			break;
		}
	}
	for (int i=1;i<=m;++i)
		if (a[i].x!=a[i-1].x)
			b[++cnt]=a[i];
//	for (int i=1;i<=m;++i)cout<<b[i].x<<" ";
	ans=1ll*ans*ksm(V,2*(b[1].x-1))%mod;
	ans=1ll*ans*ksm(V,2*(n-b[cnt].x))%mod;
	for (int i=2;i<=cnt;++i){
		int s=ksm(V,2*(b[i].x-b[i-1].x));
		s=(s+mod-1ll*ksm(V,b[i].x-b[i-1].x-1)*(V-1)%mod)%mod;
		ans=1ll*ans*s%mod;
	}cout<<ans<<"\n";
}
void init(){
	
}
int main(){
	freopen("assign.in","r",stdin);freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>T;
	while (T--)solve(),init();
}
