#include<bits/stdc++.h>
#define mems(x,y) memset(x,y,sizeof x)
#define eb emplace_back
#define Mp make_pair
#define il inline
#define gc getchar
#define pc putchar
#define rep(i,x,y) for(int i=(x);i<=(y);i++)
#define drep(i,x,y) for(int i=(x);i>=(y);i--)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
template<typename T>
il void read(T &x){
	int f=1;x=0;
	char c=gc();
	while(c<'0'||c>'9'){
		if(c=='-'){
			f=-1;
		}
		c=gc();
	}
	while(c>='0'&&c<='9'){
		x=x*10+c-48;
		c=gc();
	}
	x*=f;
}
template<typename T,typename ...Args>
void read(T &x,Args &...args){
	read(x),read(args...);
}
const int N=1e5+7,M=-1,inf=0x3f3f3f3f,mod=1e9+7;
const ll INF=1ll*inf*inf;
int n,m,k;
pii a[N];
map<int,int> mp;
il int Mod(int x,int y){
	return x+y>=mod?x+y-mod:x+y;
}
il int qpow(int x,int y){
	int ret=1;
	while(y){
		if(y&1){
			ret=1ll*ret*x%mod;
		}
		x=1ll*x*x%mod,y>>=1;
	}
	return ret;
}
void Yorushika(){
	read(n,m,k);
	mp.clear();
	bool fl=1;
	rep(i,1,m){
		read(a[i].fi,a[i].se);
		if(mp.count(a[i].fi)&&mp[a[i].fi]!=a[i].se){
			fl=0;
		}
		mp[a[i].fi]=a[i].se;
	}
	if(!fl){
		puts("0");
		return;
	}
	sort(a+1,a+m+1),m=unique(a+1,a+m+1)-a-1;
	int ans=qpow(k,2*(a[1].fi-1));
	rep(i,2,m){
		int d=a[i].fi-a[i-1].fi;
		int x=1ll*qpow(k,d-1)*(k-1)%mod,y=qpow(k,2*d);
		ans=1ll*ans*Mod(y,mod-x)%mod;
	}
	ans=1ll*ans*qpow(k,2*(n-a[m].fi))%mod;
	printf("%d\n",ans);
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t=1;
	read(t);
	while(t--){
		Yorushika();
	}
}