#include<bits/stdc++.h>
#define ll long long
#define Mod 1000000007
using namespace std;
ll n,m,v;
bool t[1000004];
ll get(ll k){
	if(k == n - 1){
		if(t[n-1] == 1 && t[n] == 1) return (v * v - v + 1) % Mod;
		else return (v * v) % Mod;
	}
	if(t[k] == 0) return (((v * v) % Mod) * get(k+1)) % Mod;
	else{
		if(t[k+1] == 0){
			t[k+1] = 1;
			ll ans = v * get(k+1) % Mod;
			t[k+1] = 0;
			return (ans + (get(k+1) * ((v * (v - 1)) % Mod)) % Mod) % Mod;
		}
		else{
			return (((v * (v - 1) + 1) % Mod) * get(k+1)) % Mod;
		}
	}
}

ll ksm(ll a,ll b,ll q){
	ll res = 1;
	a %= q , b %= q;
	while(b){
		if(b & 1) res = (res * a) % q;
		a = (a * a) % q;
		b >>= 1;
	}
	return res % q;
}
void work(){
	map<ll,ll> ma;
	cin>>n>>m>>v;
	ll g,h;
	memset(t,0,sizeof(t));
	bool f = true,A = true;
	for(int i=1;i<=m;i++){
		cin>>g>>h, t[g] = 1;
		if(g != i) A = false;
		if(ma[g]){
			if(ma[g] != h) f = false;
		}
		else ma[g] = h;
	}
	if(A && n == m){
		cout<<ksm(v * v - v + 1 , n - 1 , Mod)<<endl;
		return ;
	}
	if(m == 1){
		cout<<ksm(v , 2 * n - 2 , Mod)<<endl;
	}
	cout<<(f ? get(1) : 0)<<endl;
	ma.clear();
} 
ll h[100005];
/*void work2(){
	cin>>n>>m>>v;
	ll g;
	bool f = true;
	for(int i=1;i<=m;i++){
		cin>>h[i]>>g;
		if(ma[h[i]]){
			if(ma[h[i]] != g) f = false;
		}
		else ma[h[i]] = g;
	}
	if(f == false){
		cout<<0<<endl;
		return ;
	}
	ma.clear();
	ll ans = 0;
	sort(h+1,h+m+1);
	ll l = 1;
	for(int i=1;i<=m;i++){
		if(h[i] != h[i-1] + 1){
			ans = ans + ksm(v * v - v + 1 , h[i] - h[i-1] - 1 , Mod);
		}
		else {
			
		}ans = ans + ksm(v , 2 * n - 2 , Mod);
		ans %= Mod;
	}
}*/
ll T;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	while(T--){
		work();
		//work2();
	}
	return 0;
}
/*
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2
*/
