#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=2e5+1,mod=1e9+7;
ll n,m,k;
ll a[N];
map<ll,ll>A;
inline ll qmi(ll x,ll y){
	x%=mod;
	ll res=1;
	while(y>0){
		if(y&1)
			res=res*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return res;
}
void solve(){
	cin>>n>>m>>k;
	A.clear();
	bool ok=1;
	int res=0;
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		if(A[x]&&A[x]!=y){
			ok=0;
		}
		if(A[x]!=y)
			a[++res]=x;
		A[x]=y;
	}
	if(!ok){
		cout<<0<<"\n";
		return ;
	}
	sort(a+1,a+res+1);
	ll ans=1;
	if(a[1]!=1)
		ans=qmi(k*k%mod,a[1]-1);
	for(int i=1;i<res;i++)
		ans=ans*(qmi(k*k%mod,a[i+1]-a[i])-(k-1)*qmi(k,a[i+1]-a[i]-1)%mod+mod)%mod;
	if(a[res]!=n)
		ans=ans*qmi(k*k,n-a[res])%mod;
	cout<<ans<<"\n";
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
	int t;
	cin>>t;
	while(t--)
		solve();
	return 0;
}
