#include <bits/stdc++.h>
#define fail {cout<<"0\n";return;}
using namespace std;
constexpr long long M=1e5+1,P=1e9+7;
using pii=pair<int,int>; 
int n,m,v;
pii lim[M];
long long qpow(long long a,int b){
	long long res=1;
	while (b){
		if (b&1) (res*=a)%=P;
		(a*=a)%=P;
		b>>=1;
	}
	return res;
}
long long ans=0;
void solve(){
	cin>>n>>m>>v;
	for (int i=0,c,d;i<m;i++)
		cin>>c>>d,lim[i]={c,d};
	sort(lim,lim+m);
	for (int i=1;i<m;i++)
		if (lim[i].first==lim[i-1].first&&lim[i].second!=lim[i-1].second)
			fail;
	long long ans=qpow(qpow(v,2),lim[0].first-1);
	for (int i=1;i<m;i++){
		if (lim[i].first==lim[i-1].first)continue;
		long long f1=qpow(qpow(v,2),lim[i].first-lim[i-1].first);
		long long f2=qpow(v,lim[i].first-lim[i-1].first-1)*(v-1)%P;
		(ans*=(f1-f2+P))%=P;
	}
	(ans*=qpow(qpow(v,2),n-lim[m-1].first))%=P;
	cout<<ans<<'\n';
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin.tie(0),cout.tie(0)->sync_with_stdio(0);
	int t;cin>>t;
	while (t--)
		solve();
}

