#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+1,mod=1e9+7;
int T,n,m,v,ans;
struct node{
	int x,w;
}a[N];
bool cmp(node a,node b){
	if(a.x!=b.x) return a.x<b.x;
	return a.w<b.w;
}
int fpow(int x,int k){
	if(k==0) return 1;
	int tmp=fpow(x,k/2),res=1;
	if(k%2) res=x;
	return res*tmp%mod*tmp%mod;
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&T);
	while(T--){
		scanf("%lld%lld%lld",&n,&m,&v);
		for(int i=1;i<=m;i++)
			scanf("%lld%lld",&a[i].x,&a[i].w);
		sort(a+1,a+1+m,cmp),ans=fpow(v,(n-a[m].x)*2)%mod;
		if(a[1].x>1)
			ans=ans*fpow(v,(a[1].x-1)*2)%mod;
		for(int i=1;i<m;i++){
			if(a[i].x==a[i+1].x){
				if(a[i].w!=a[i+1].w){
					ans=0;
					break;
				}
				continue;
			}
			ans=ans*((fpow(v,(a[i+1].x-a[i].x)*2)-fpow(v,a[i+1].x-a[i].x-1)*(v-1)%mod+mod)%mod)%mod;
		}
		printf("%lld\n",ans); 
	}
	return 0;
}
