#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef __int128 i128;

LL read() {
	LL sum=0,flag=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') flag=-1; c=getchar();}
	while(c>='0'&&c<='9') {sum=sum*10+c-'0'; c=getchar();}
	return sum*flag;
}

const LL MOD=1e9+7;
const int N=1e5+10;
int n,m;
LL v;
struct node {
	int c,d;
}s[N];

int cmp(node x,node y) {
	return x.c<y.c;
}

LL ksm(LL a,LL b) {
	LL s=1;
	while(b) {
		if(b&1) s=s*a%MOD;
		a=a*a%MOD;
		b>>=1;
	}
	return s;
}

void Sol() {
	n=read(); m=read(); v=read();
	unordered_map<int,int> mp;
	int flag=1;
	for(int i=1;i<=m;i++) {
		int c=read(),d=read();
		if(mp[c]&&mp[c]!=d) {
			flag=0;
		}
		s[i]={c,d};
		mp[c]=d;
	}
	
	if(!flag){
		cout<<0<<'\n'; return ;
	}

	if(v==1) {
		cout<<1<<'\n'; return ;
	}
	
	sort(s+1,s+1+m,cmp);
	LL ans=1;
	int p=s[1].c-1;
	if(p) {
		ans*=ksm(v,2*p); 
	}
	p=s[m].c; 
	if(p<=n-1) {
		ans*=ksm(v,2*(n-p)); ans%=MOD; 
	}
	for(int i=1;i<m;i++) {
		if(s[i+1].c==s[i].c) continue;
		LL cnt=0;
		cnt+=ksm(v,2*(s[i+1].c-s[i].c));
		cnt-=ksm(v,s[i+1].c-s[i].c-1)*(v-1)%MOD;
		cnt%=MOD;
		ans=ans*cnt%MOD; 
	}
	cout<<(ans%MOD+MOD)%MOD<<'\n';
}

void Clear() {
	
}

int main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);

	int T=read();
	while(T--) {
		Sol();
		Clear();
	}
	

	return 0;
}
/*
1.判断一个位置会不会重复  
*/




















