#include<bits/stdc++.h>
using namespace std;

const int mod=1e9+7;

int T;
int n,m,v;
struct node{
	int c,d;
};
vector<node>ass;

int ksm(int x,int p){
	int res=1;
	while(p){
		if(p&1){
			res=1ll*res*x%mod;
		}
		x=1ll*x*x%mod;
		p>>=1;
	}
	return res;
}

int solve(){
	
	sort(ass.begin(),ass.end(),[&](node x,node y){
		return x.c<y.c;
	});
	
	if(v==1){
		return 1;
	}
	
	for(int i=1;i<m;i++){
		if(ass[i].c==ass[i-1].c&&ass[i].d!=ass[i-1].d){
			return 0;
		}
	}
	
	int res=1ll*ksm(v,(ass.front().c-1)<<1)*ksm(v,(n-ass.back().c)<<1)%mod;
	
	for(int i=1;i<m;i++){
		if(ass[i].c==ass[i-1].c) continue;
		int cnt=ass[i].c-ass[i-1].c;
		int tmp=ksm(v,cnt<<1);
		tmp=(tmp-1ll*ksm(v,cnt-1)*(v-1)%mod+mod)%mod;
		res=1ll*res*tmp%mod; 
	}
	
	return res;
	
}

int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin>>T;
	
	while(T--){
		cin>>n>>m>>v;
		ass.resize(m);
		for(auto &i:ass){
			cin>>i.c>>i.d;
		}
		
		cout<<solve()<<"\n";
		
	}
	
	return 0;
} 
