#include <bits/stdc++.h>
using namespace std;
#define N 100005
#define int long long
int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<3)+(x<<1)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
const int mo=1000000007;
int qpow(int x,int y){
	int res=1;
	while(y){
		if(y&1) res=res*x%mo;
		x=x*x%mo,y>>=1;
	}
	return res;
}
int n,m,v,ans;
struct node{
	int id,x;
	bool operator <(const node &A) const{
		return id==A.id?x<A.x:id<A.id;
	}
}a[N];
void work(){
	n=read(),m=read(),v=read(),ans=1;
	for(int i=1;i<=m;i++){
		a[i].id=read(),a[i].x=read();
	}
	sort(a+1,a+m+1);
	int fi=a[1].id;
	ans=ans*qpow(v,2*(fi-1))%mo;
	for(int i=1;i<m;i++){
		if(a[i].id==a[i+1].id&&a[i].x==a[i+1].x) continue;
		if(a[i].id==a[i+1].id&&a[i].x!=a[i+1].x){
			printf("0\n");
			return ;
		}
		int d=a[i+1].id-a[i].id;
		int cnt=qpow(v*v%mo,d)+mo-qpow(v,d-1)*(v-1)%mo;
		cnt=cnt%mo;
		ans=ans*cnt%mo;
	}
	int lst=a[m].id;
	ans=ans*qpow(v,2*(n-lst))%mo;
	printf("%lld\n",ans);
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T=read();
	while(T--) work();
	return 0;
}

