#include<bits/stdc++.h>
#define LL long long
#define Maxn 100005
using namespace std;
const int md = 1000000007;
LL ksm(LL x, LL y){
	LL z=1;
	while(y){
		if(y&1) z=z*x%md;
		x=x*x%md; y>>=1;
	}
	return z;
}

int n,m;
LL V,ans;

struct node{
	int u,v;
}a[Maxn];
bool cmp(node x, node y){ return x.u < y.u; }
int b[Maxn],tot;

void slv(){
	scanf("%d%d%lld",&n,&m,&V);
	for(int i=1; i<=m; i++){
		scanf("%d%d",&a[i].u,&a[i].v);
	}
	
	sort(a+1, a+m+1, cmp);
	for(int i=1; i<m; i++)if(a[i].u==a[i+1].u && a[i].v!=a[i+1].v){
		printf("0\n"); return;
	}
	tot = 0;
	for(int i=1; i<=m; i++){ b[i] = a[i].u; }
	tot = unique(b+1, b+m+1) - b - 1;
	
	ans = ksm(V, (b[1]-1 + n-b[tot]) << 1);
	
	for(int i=1; i<tot; i++){
		(ans *= (ksm(V,(b[i+1]-b[i])<<1) - ksm(V,b[i+1]-b[i]-1)*(V-1)%md)) %= md;
	}
	printf("%lld\n", (ans + md) % md);
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int ttt; scanf("%d",&ttt);
	while(ttt--) slv();
	return 0;
}

