#include<algorithm>
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
using namespace std;
const int N=2e5+10,mod=1e9+7;
int n,m,v;
int fpow(int a,int b) {
	int res=1;
	for(; b; b>>=1,a=1ll*a*a%mod)
		if(b&1) res=1ll*res*a%mod;
	return res;
}
map<int,int> a;
void solve() {
	a.clear();
	scanf("%d%d%d",&n,&m,&v);
	bool flag=1;
	for(int i=1; i<=m; i++) {
		int x,y; scanf("%d%d",&x,&y);
		if(a[x]&&a[x]!=y) flag=0;
		a[x]=y;
	}
	if(!flag) return printf("0\n"),void();
	int las=0,ans=1;
	for(pair<int,int> it:a) {
		int x=it.first;
		if(las==0) ans=1ll*ans*fpow(v,2*(x-1))%mod,las=x;
		else {
			ans=1ll*ans*(fpow(v,2*(x-las))-1ll*fpow(v,x-las-1)*(v-1)%mod+mod)%mod;
			las=x;
		}
	}
	if(las!=n) ans=1ll*ans*fpow(v,2*(n-las))%mod;
	printf("%d\n",ans);
};
int main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t; scanf("%d",&t);
	for(; t; t--) solve();
	return 0;
}
