#include <bits/stdc++.h> 
using namespace std;
#define fastIO ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define int long long
const int N = 100003;
const int mod = 1000000007;
int power(int x,int y){
	int ret=1;
	while(y){
		if(y&1) ret=(ret*x)%mod;
		y/=2; x=(x*x)%mod;
	}
	return ret;
}
int T,n,m,k,sq;
int ans;
struct node{
	int x,y;
	bool operator < (const node& B) const{
		return x<B.x;
	}
}a[N];
void Solve(){
	cin >> n >> m >> k; sq=k*k%mod;
	for(int i=1;i<=m;i++){
		cin>>a[i].x >> a[i].y;
	}
	sort(a+1,a+m+1);
	for(int i=2;i<=m;i++){
		if((a[i].x==a[i-1].x)&&(a[i].y!=a[i-1].y)){
			cout << 0 << endl;
			return;
		} 
	}
	for(int i=1;i<=m;i++){
		if(a[i].x==a[i-1].x) continue;
		if(i==1) ans=power(sq,a[i].x-1);
		else ans=(ans*(((power(sq,a[i].x-a[i-1].x)-(power(k,a[i].x-a[i-1].x-1)*(k-1)%mod))%mod+mod)%mod+mod)%mod);
	}
	ans=(ans*power(sq,n-a[m].x)%mod+mod)%mod;
	cout << ans << endl;
}
signed main(){ freopen("assign.in","r",stdin); freopen("assign.out","w",stdout); fastIO;
	cin >> T;
	while(T--) Solve();
	return 0;
}
