#include<bits/stdc++.h>
#define rg register int
#define fo(i,l,r) for(rg i=(l);i<=(r);i++)
#define dfo(i,r,l) for(rg i=(r);i>=(l);i--)
#define fe(i,x) for(rg i=hd[x];i;i=nex[i])
#define frein freopen("in.txt","r",stdin)
#define freout freopen("out.txt","w",stdout)
#define fre(p) freopen(#p".in","r",stdin),freopen(#p".out","w",stdout)
#define outa(l,r,a) {fo(i,l,r)cout<<a[i]<<" ";cout<<"\n";}
#define eb emplace_back
#define ll long long
using namespace std;
void rd(int &x){
	int v(0),op(1);
	register char c=getchar();
	while(c<'0'||'9'<c)op=c=='-'?-1:1,c=getchar();
	while('0'<=c&&c<='9')v=(v<<1)+(v<<3)+(c^48),c=getchar();
	x=v*op; 
}
#define mo 1000000007
int ksm(int x,int y){
	if(y<0)return 1;
	int ans=1;
	while(y){
		if(y&1)ans=1ll*ans*x%mo;
		x=1ll*x*x%mo,y>>=1;
	}return ans;
}
void inc(int &x,int y){x+=y;if(x>=mo)x-=mo;}
void dec(int &x,int y){x-=y;if(x<0)x+=mo;}
const int N=1e5+5;
int T,n,m,k,ans0,ans1,tp,t0,t1,ans;
struct nd{int p,v;}a[N];
int c[N],d[N],h[N];
bool pd;
void dfs2(int x){
	if(x==n+1){
		bool is=0;
		fo(i,1,n-1){
			if(h[i]==c[i]&&d[i]!=h[i+1]){
				is=1;
			}
		}
		if(is==0)pd=1;
		return;
	}
	fo(i,1,n)h[x]=i,dfs2(x+1);
}
void dfs(int x){
	if(x==n){
		dfs2(1);
		if(pd)ans++;
		return;
	}
	fo(i,1,k)
	fo(j,1,k)c[x]=i,d[x]=j,dfs(x+1);
}
void solve(){
	rd(n),rd(m),rd(k);
	fo(i,1,m)rd(a[i].p),rd(a[i].v);
	sort(a+1,a+1+m,[](nd a,nd b){return a.p<b.p;});
	fo(i,2,m){
		if(a[i].p==a[i-1].p&&a[i].v!=a[i-1].v){
			cout<<"0\n";
			return;
		}
	}
//	ans0=1ll*ksm(k,2*a[1].p-1)*(k-1)%mo;
//	ans1=ksm(k,2*a[1].p-1);
////	cout<<ans0<<" "<<ans1<<"\n";
//	fo(i,2,m){
//		if(a[i].p==a[i-1].p)continue;
//		if(a[i].p==n){
//			tp=ksm(k,2*(a[i].p-a[i-1].p-1));
//			t0=1ll*ans0*tp%mo*(k-1)%mo;
//			t1=1ll*ans0*tp%mo;
//			inc(t1,1ll*ans1*tp%mo);
//			dec(tp,1ll*ksm(k,a[i].p-a[i-1].p-2));
//			inc(t0,1ll*ans1*tp%mo*(k-1)%mo);
//		}
//		else{
//			tp=ksm(k,2*(a[i].p-a[i-1].p)-1);
//			t0=1ll*ans0*tp%mo*(k-1)%mo;
//			t1=1ll*ans0*tp%mo;
//			inc(t1,1ll*ans1*tp%mo);
//			dec(tp,1ll*ksm(k,a[i].p-a[i-1].p-1));
//			inc(t0,1ll*ans1*tp%mo*(k-1)%mo);
//		}
//		ans0=t0,ans1=t1;
////		cout<<tp<<" "<<ans0+ans1<<" "<<ans0<<" "<<ans1<<"\n";
//	}
//	if(n-a[m].p-1>=0){
//		ans0=1ll*ans0*ksm(k,2*(n-a[m].p-1))%mo;
//		ans1=1ll*ans1*ksm(k,2*(n-a[m].p-1))%mo;
//	}
//	inc(ans1,ans0);
//	cout<<ans1<<"\n";
	ans=ksm(k,2*a[1].p);
	fo(i,2,m){
		if(a[i].p==a[i-1].p)continue;
		if(a[i].p==n){
			tp=ksm(k,2*(a[i].p-a[i-1].p-1));
			dec(tp,1ll*ksm(k,a[i].p-a[i-1].p-2)*(k-1)%mo);
		}
		else{
			tp=ksm(k,2*(a[i].p-a[i-1].p));
			dec(tp,1ll*ksm(k,a[i].p-a[i-1].p-1)*(k-1)%mo);
		}
		ans=1ll*ans*tp%mo;
//		cout<<tp<<" "<<ans<<"\n";
	}
	if(n-a[m].p-1>=0)ans=1ll*ans*ksm(k,2*(n-a[m].p-1))%mo;
	cout<<ans<<"\n";
}
int main(){
//	frein;
	fre(assign);
	rd(T);
	while(T--)solve();
	return 0;
}
