#include <bits/stdc++.h>
using namespace std;
#define F(i,a,b) for(int i=(a);i<=(b);i++)
#define F_(i,a,b) for(int i=(a);i>=(b);i--)
typedef long long ll;
const int N = 1e5+10;
const ll mod = 1e9+7;
int t;
int n,m,v;
ll ans;
struct XZ{
	int pos,val;
} xz[N];
bool cmp(XZ a,XZ b){return a.pos<b.pos;}
bool wujie(){
	F(i,2,m) if (xz[i].pos==xz[i-1].pos&&xz[i].val!=xz[i-1].val) return 1;
	return 0;
}
void init(){
	ans = 1;
}
ll ksm(ll x,int y){
	ll sum=1;
	while (y){
		if (y&1) sum = (sum*x)%mod;
		x = (x*x)%mod;
		y >>= 1;
	}
	return sum;
}
void work(){
	init();
	scanf("%d%d%d",&n,&m,&v);
	F(i,1,m) scanf("%d%d",&xz[i].pos,&xz[i].val);
	sort(xz+1,xz+m+1,cmp);
	if (wujie()){printf("0\n");return;}
	ans *= ksm(v,(xz[1].pos-1+n-xz[m].pos)*2);
	F(i,2,m){
		int dis = xz[i].pos-xz[i-1].pos-1;
		if (dis==-1) continue;
		ans = (ans*(ksm(v,(dis+1)*2)-ksm(v,dis)*(v-1)%mod+mod)%mod)%mod;
	}
	printf("%lld\n",ans);
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&t);
	while (t--) work();
	return 0;
}
