#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define rg register
#define fo(i,l,r) for(rg int i(l);i<=(r);++i)
#define rp(i,r,l) for(rg int i(r);i>=(l);--i)
#define pb push_back
#define pc putchar
#define gc getchar
//#define int long long
using namespace std;

const int M=1e5+5;
const int mod=1e9+7;

int T,n,m,v,b[M];

struct L { int u,v; }a[M];

int qp(int a,int b=mod-2) {
	int s=1;while(b) {
		if(b&1) s=1ll*s*a%mod;
		a=1ll*a*a%mod;b>>=1;
	} return s;
}
bool check() {
	fo(i,2,m) {
		if(a[i].u==a[i-1].u&&a[i].v!=a[i-1].v) {
			puts("0");
			return 1;
		}
	}
	return 0;
}

inline void wt(rg int x) {
	if(x<0) pc('-'),x=-x;
	if(x>9) wt(x/10);
	pc(x%10+48);
}
inline int re() {
	rg int x=0,p=1;rg char c=gc();
	while(!isdigit(c)) {
		if(c=='-') p=-1;
		c=gc();
	}
	while(isdigit(c)) (x*=10)+=c-48,c=gc();
	return x*p;
}

int main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	T=re();
	while(T--) {
		n=re(),m=re(),v=re();
//		cerr<<n<<" "<<m<<" "<<endl;
		fo(i,1,m) a[i].u=re(),a[i].v=re();
		sort(a+1,a+m+1,[](L a,L b){return a.u<b.u;});
		if(check()) continue;
		
		int l=0;
		fo(i,1,m) if(a[i].u!=a[i-1].u) b[++l]=a[i].u;
		
		int f=qp(v,2*(b[1]-1));
		fo(id,2,l) {
			int i=b[id];
			int j=b[id-1];
			f=(1ll*f*qp(v,2*(i-j))%mod-1ll*f*qp(v,i-j-1)%mod*(v-1)%mod+mod)%mod;
		}
		f=1ll*f*qp(v,2*(n-b[l]))%mod;
		wt(f);pc('\n');
	}
}

