#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll Mod=1e9+7;
const int M=1e5+10;
int T;
ll n,m,v,c[M],d[M];
map<ll,ll> mp;
ll ans=0,dif=0;
ll qpow(ll a,ll b){
	ll res=1,base=a;
	while(b){
		if(b&1) res*=base,res%=Mod;
		base*=base,base%=Mod;
		b>>=1;
	}
	return res;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--){
		mp.clear();
		scanf("%lld %lld %lld",&n,&m,&v);
		for(int i=1;i<=m;i++) scanf("%lld %lld",&c[i],&d[i]);
		ans=qpow(v,2*n-2),dif=0;
		for(int i=1;i<=m;i++){
			if(mp[c[i]]){
				if(mp[c[i]]==d[i]) continue;
				else{
					ans=0;
					break;
				}
			}
			mp[c[i]]=d[i];
			if(mp[c[i]-1]) dif+=v-1,dif%=Mod;
			if(mp[c[i]+1]) dif+=v-1,dif%=Mod;
		}
		if(!ans) printf("0\n");
		else printf("%lld\n",(ans-dif+Mod)%Mod);
	}
	return 0;
}
