#include<bits/stdc++.h>
using namespace std;
#define MaxM 100010
#define M 1000000007

long long power_record[100];
long long power_get(int a,int k){
	if(k==0)	return a%M;
	if(power_record[k])	return power_record[k];
	power_record[k]=(power_get(a,k-1)*power_get(a,k-1))%M;
	return power_record[k];
}
long long quick_power(long long a,long long k){
	//cout<<"quick_power:"<<a<<" "<<k<<endl;
	long long res=1;
	for(long long i=0;k>>i;i++){
		if(k>>i&1)
			res=(res*power_get(a,i))%M;
		
	}
	return res;
}

long long n,m;
long long v;
long long ans;
pair<long long,long long>	plan[MaxM];
long long d[MaxM],tot=0;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;cin>>T;while(T--){
		memset(power_record,0,sizeof(power_record));
		memset(d,0,sizeof(d));
		cin>>n>>m>>v;
		ans=1,tot=0;
		for(int i=1;i<=m;i++)	cin>>plan[i].first>>plan[i].second;
		sort(plan+1,plan+1+m);
		bool flag=false;
		for(int i=1;!flag&&i<=m;i++)
			if(plan[i].first==plan[i-1].first&&plan[i].second!=plan[i-1].second)
				flag=true;
		if(flag){
			cout<<"0"<<endl;
			continue;
		}
		//cout<<"access"<<endl;
		for(int i=1;i<=m;i++)	if(plan[i].first!=plan[i-1].first) d[++tot]=plan[i].first;
		for(int i=1;i<=tot-1;i++){
			long long a1=quick_power(v,2*(d[i+1]-d[i]));
			long long a2=(quick_power(v,d[i+1]-d[i]-1)*(v-1))%M;
			while(a1<a2)	a1+=M;
			ans=(ans*(a1-a2))%M;
		}
		//cout<<"ans now is:"<<ans<<endl;
		ans=(ans*quick_power(v,(d[1]-1)*2))%M;
		ans=(ans*quick_power(v,(n-d[tot])*2))%M;
		cout<<ans<<endl;
	}
	return 0;
}
