#include <bits/stdc++.h>
#define int long long  
#define MOD 1000000007
#define N 100005
using namespace std;
int T,n,m,v;
struct note{
	int id,val,fl;
}a[N];
bool cmp (const note x,const note b){
	return x.id<b.id;
}
bool cmp2( const note x,const note b){
	if(x.fl<b.fl){
		return 1;
	}
	else if(x.fl>b.fl){
		return 0;
	}
	else{
		return x.id<b.id;
	}
}
int ans = 0;
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	while(T--){
		ans = 0;
		scanf("%lld%lld%lld",&n,&m,&v);
		for(int i = 1;i<=m;i++){
			int t1,t2;	scanf("%lld%lld",&t1,&t2);
			a[i].id = t1;
			a[i].val = t2;
			a[i].fl = 0;		
		}
				if(n == m){
			int tmp2 = ((v-1)*v)%MOD;
		tmp2+=1;
		ans = 1;
		m-=1;
		while(m--){
			ans*=tmp2;
			ans%=MOD; 
		}
		cout<<ans<<endl;
		continue;
		}
		ans = 1;sort(a+1,a+1+m,cmp);

		bool fla = 0;
		int tmpp = 0;
		for(int i= 1;i<=m;i++){
			if(a[i-1].id == a[i].id){
				if(a[i-1].val != a[i].val){
					cout<<0<<endl;
					fla = 1;
				}
				else{
					tmpp+=1;
					a[i].fl = 1;
				}
			}
		}
		if(fla)continue;
		sort(a+1,a+1+m,cmp2);
		//查重
		m-=tmpp;
		int nn = n-1;
		//边数 
		int lastid = a[1].id;
		for(int i = 2;i<=m;i++){
			int ansp = 1;
			int tmp = a[i].id-lastid;//边数 
			nn-=tmp;
			// 
			if(tmp == 1){
				ansp = v-1;
			}
			else if(tmp ==2){
				ansp = (v-1)*(v-1);
				ansp%=MOD;
			}
			else{
				while(tmp!=2&&tmp>0){ 
					ansp*=v;
					ansp%=MOD;
					tmp--;
				}
				ansp*=(v-1);
				ansp%=MOD;
				ansp*=(v-1);
				ansp%=MOD;
			}
			//计算出不符合的情况
			tmp = a[i].id-lastid;
			int tmp2 = 1;
			int v1 = v*v;
			v1%=MOD;
			while(tmp>0){
				tmp2*=v1;
				tmp2%=MOD;
				tmp--;
			} 
			//计算出符合的情况
			ans *= (tmp2-ansp); 
			ans%=MOD;
			lastid = a[i].id; 
		}
		while(nn>0){
			ans*=v;
			ans%=MOD;
			ans*=v;
			ans%=MOD;
			nn--;
		}
		cout<<ans<<endl;
	}
	return 0;
}
