#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
bool flag=1;
long long T,n,m,v,cnt,ans;
long long qp(long long a,long long b){
	long long ret=1;
	while(b){
		if(b&1)	ret=ret*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ret;
}
struct node{
	long long p,num;
}o[100010];
bool cmp(node a,node b){
	return a.p<b.p;
}
void solve(){
	int now=0;
	for(int i=1;i<=m;i++){
		if(o[i].p==o[i-1].p){
			if(o[i].num==o[i-1].num){
				continue ;
			}
			ans=0;
			return ;
		}
		if(now)	ans=ans*(1+(v-1)*v%mod)%mod;
		ans=ans*qp(v*v%mod),(o[i].p-o[i-1].p-1))%mod;
		now=i;
	}
	ans=ans*(1+(v-1)*v%mod)%mod;
	ans=ans*qp(v*v%mod),n-o[m].p-1)%mod;
}
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;
	//cout<<qp(44020*44019%mod+1,97157)<<endl;
	while(T--){
		cin>>n>>m>>v;
		ans=1;
		for(int i=1;i<=m;i++){
			cin>>o[i].p>>o[i].num;
			if(o[i].p!=i)	flag=0;
		}
		if(flag){
			long long x=v*(v-1)%mod+1,y=n-1;
			cout<<qp(x,y)%mod<<"\n";
			continue ;
		}
		sort(o+1,o+1+m,cmp);
		solve();
		cout<<ans<<"\n";
		flag=1;
	}
	return 0;
}
