#include<algorithm> 
#include<iostream> 
#include<cstdio> 
#include<string> 
#include<math.h> 
#include<map>
#define int long long
using namespace std;
const int N=2e5+10;
const int M=1e5+10;
const int MOD=1e9+7;
int n,m,k,c[N],d[N];
int poww(int a,int b){
	int ans=1;
	a%=MOD;
	while(b){
		if(b&1)ans*=a;
		a*=a;
		b>>=1;
		a%=MOD;
		ans%=MOD;
	}
	return ans;
}
bool checkA(){
	return 1;
}
void solveA(){
	return ;
}
bool checkB(){
}
bool check0(){
	
	for(int i=1;i<m;i++){
		if(c[i]==c[i+1]&&d[i]!=d[i+1])return 1;
	}
	return 0;
}
void solveB(){
	return ;
}
void solvem(){
	int x=k*k;
	cout<<poww(x,n-1)<<'\n';
	return ;
}
struct node{
	int cc,dd;
}cd[N];
bool cmp(node a,node b){
	return a.cc<b.cc;
}
void SOLVE(){
	cin>>n>>m>>k;
	for(int i=1;i<=m;i++){
		cin>>cd[i].cc>>cd[i].dd;
	}
	sort(cd+1,cd+m+1,cmp);
	for(int i=1;i<=m;i++){
		c[i]=cd[i].cc;
		d[i]=cd[i].dd;
	}
	
	if(check0()){
		cout<<"0\n";
		return ;
	}
	int t=0;
	for(int i=1;i<=m;i++){
		if(c[i]==c[t])continue;
		c[++t]=c[i];
		d[t]=c[i];
		//cout<<c[i]<<" ";
	}//cout<<'\n';
	m=t;
	
	
	if(m==1){
		solvem();
		return ;
	}
	if(k==1){
		cout<<1<<"\n";
		return ;
	}
	
	int ans=1,ans2=1,ans1=1;
	ans*=poww(k*k,c[1]-1);
	ans%=MOD;
	ans*=poww(k*k,n-c[m]);
	ans%=MOD;
	for(int i=1;i<m;i++){
		if(c[i]+1==c[i+1]){
			t=k*(k-1)+1;
			t%=MOD;
			ans*=t;
			ans%=MOD;
		}else{
			ans*=poww(k*k,c[i+1]-c[i]);
			ans%=MOD;
		}
		/*int l=c[i+1]-c[i];
		int t1=(k-1)*(k-1);
		t1%=MOD;
		int t=poww(k*k,l-1);
		t%=MOD;
		t*=t1;
		t%=MOD;
		ans*=t;
		ans%=MOD;
		ans1*=t;
		ans1%=MOD;
		ans2*=t;
		ans2%=MOD;
		ans2*=t1;
		ans2%=MOD;*/
	}
	/*ans*=ans1;
	ans%=MOD;
	ans*=ans2;
	ans%=MOD;*/
	cout<<ans<<"\n";
	return ;
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	cin>>T;
	while(T--){
		//cout<<T<<"\n";
		SOLVE();
	}
	return 0;
}
/*
1
10 5 2
2 2
3 2
4 2
7 2
10 2
*/
/*
2
5 5 2
1 2
2 2
3 2
4 2
5 2
5 5 2
1 2
2 2
2 2
4 2
5 2
*/
