#include<bits/stdc++.h>
#define il inline
#define int long long
using namespace std;
#define gc() getchar()
il int rd(){
	int x=0,f=1;
	char ch=gc();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}
	while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=gc();
	return x*f;
}
/*
n^2 dp 显然 
数数的是(a,b)的方案，而不是x
那么如果没有m的单个赋值限制显然任意(a,b)都可以 
考虑赋值后产生什么影响 
1.xi!=ai 没影响
2.xi=ai 相当于限制了x i+1=bi
对于ai+1产生连锁 
*/
const int N=1e6+10,mod=1e9+7;
int n,m,k;
int ksm(int x,int y){
	int s=1;
	while(y){
		if(y&1)s=1ll*s*x%mod;
		x=1ll*x*x%mod,y>>=1;
	}
	return s;
}
struct dat{
	int c,d;
}a[N];
int ran[N];
void Main(){
	n=rd(),m=rd(),k=rd();
	int tot=0;
	for(int i=1; i<=m; ++i){
		a[i].c=rd(),a[i].d=rd();
		ran[++tot]=a[i].c;
	}
	sort(a+1,a+1+m,[&](dat A,dat B){return A.c<B.c;});
	for(int i=1; i<m; ++i){
		if(a[i].c==a[i+1].c&&a[i].d!=a[i+1].d){puts("0");return;}
	}
	sort(ran+1,ran+1+tot);
	int len=unique(ran+1,ran+1+tot)-ran-1;
	int ans=1;
	for(int i=2; i<=len; ++i){
		int pre=ran[i-1],now=ran[i]-1;
		//(a,b) i\in [pre,now]
		int w=0;
		w=(w+ksm(1ll*k*k%mod,now-pre+1))%mod;
		w=(w-1ll*ksm(k,now-pre)*(k-1)%mod+mod)%mod;
//		cout<<w<<' '<<k*(k-1)+1<<endl;
		ans=1ll*ans*w%mod;
	}
	ans=1ll*ans*ksm(1ll*k*k%mod,ran[1]-1+n-ran[len])%mod;
	printf("%lld\n",ans);
//	printf("Q%lld\n",ksm(k*(k-1)+1,n-1));
}
#undef int 
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T=rd();
	while(T--)Main();

	return 0;
}

