#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
const int N=1e5+5;
const ll mod=1e9+7;
ll qread(){
	ll 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<<1)+(x<<3)+ch-48ll;
		ch=getchar();
	}
	return x*f;
}
void qwrite(ll x){
	if(x<0) x=-x,putchar('-');
	if(x>9) qwrite(x/10);
	putchar(x%10+48);
}
ll t,n,m,v,ans;
map<ll,ll> mp;
ll fpow(ll a,ll b){
	ll res=1;
	while(b){
		if(b&1ll) (res*=a)%=mod;
		(a*=a)%=mod;
		b>>=1;
	}
	return res;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	t=qread();
	while(t--){
		n=qread();m=qread();v=qread();
		ll x,w;bool f=0;
		mp.clear();
		for(int i=1;i<=m;++i){
			x=qread();w=qread();
			if(mp.find(x)!=mp.end()&&mp[x]!=w) f=1;
			else mp[x]=w;
		}
		if(f){
			putchar('0');putchar('\n');
			continue;
		}
		ll tmp=0;
//		ans=fpow(v*v%mod,n-1ll);
		for(auto it=mp.begin(),lst=it;it!=mp.end();++it){
			if(lst==it) continue;
			if(it->first==lst->first+1ll) ++tmp;
//			ll tmp=(it->first)-(lst->first)-1;
//			ans=(ans-fpow(v,tmp)*fpow(v,n-tmp-1)%mod*(v-1ll)%mod+mod)%mod;
			lst=it;
		}
		qwrite(fpow(v*v%mod,n-tmp-1ll)*fpow((v*(v-1)%mod+1ll)%mod,tmp)%mod);
//		qwrite(ans);
		putchar('\n');
	}
	return 0;
}
/*
1
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2

2 3 4 7 10
2 2 2 2 2

*/
