#include <bits/stdc++.h>

#define int long long

#define FRR(file) freopen(file,"r",stdin)
#define FRW(file) freopen(file,"w",stdout)
#define _rep(i,a,b) for (int i=(a);i<=(b);++i)
#define _rrep(i,a,b) for (int i=(a);i>=(b);--i)
#define _reps(i,a,b,c) for (int i=(a);i<=(b);c)
#define _rreps(i,a,b,c) for (int i=(a);i>=(b);c)
#define _graph(i,u) for (int i=h[u];~i;i=ne[i])
#define _iter(it,a) for (auto it=a.begin();it!=a.end();++it)
#define LL long long

using namespace std;

const int mod=1e9+7;

int T,n,m,v;
unordered_map<int,int> hs;
vector<int> vec;

int cmp(const int& a,const int& b) {
	return a<b;
}

int qpow(int a,int b) {
	if (b==0) return 1;
	else if (b==1) return a%mod;
	int k=qpow(a,b>>1)%mod;
	if (b&1) return (k*k%mod)*a%mod;
	else return k*k%mod;
}

signed main() {
	FRR("assign.in");
	FRW("assign.out");
	scanf("%lld",&T);
	while (T--) {
		hs.clear();
		vec.clear();
		scanf("%lld %lld %lld",&n,&m,&v);
		bool ok=true;
		while (m--) {
			int c,d;
			scanf("%lld %lld",&c,&d);
			if (hs.count(c) && hs[c]!=d) ok=false;
			hs[c]=d;
		}
		if (!ok) {
			puts("0");
			continue;
		}
		_iter(it,hs) vec.push_back(it->first);
		sort(vec.begin(),vec.end(),cmp);
		int las=n;
		int ans=1;
		_rrep(i,(int)vec.size()-1,0) {
			if (vec[i]==n) continue;
//			cout<<"test: "<<vec[i]<<endl;
			int k=qpow(v,(las-vec[i])<<1);
			if (las!=n || vec.back()==n) k-=qpow(v,las-vec[i]-1)*(v-1);
			k=(k%mod+mod)%mod;
			ans*=k;
			ans%=mod;
			las=vec[i];
//			cout<<vec[i]<<" "<<ans<<endl;
		}
		if (vec[0]!=1) {
			ans*=qpow(v,(las-1)<<1);
			ans%=mod;
		}
		printf("%lld\n",ans);
	}
	return 0;
}

/*
1
817800451 1 578482625
689892411 37963033
/*

/*
1
3 3 3
1 1
2 1
3 1
ans: 49
*/

/*
1
4 2 2
1 2
4 2
*/

/*
1
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2
*/

/*
1
3 2 2
1 1
3 1
ans: 14
*/

/*
1
5 1 2
1 2
ans: 256

1
7 1 2
4 1
ans: 4096
*/
