#include<bits/stdc++.h>
using namespace std;
const int modd = 1e9+7;
long long ans,t,n,m,v,c[10000001],d[10000001],x[10000001];
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin >> t;
	while(t--){
		cin >> n >> m >> v;
		int flag = 1,flag1 = 1;
		ans = 1;
		memset(x,0,sizeof(x));
		for(long long i = 1; i <= m; i++){
			cin >> c[i] >> d[i];
			if(x[c[i]] != d[i]&&x[c[i]] != 0)	flag1 = -1;
			else	x[c[i]] = d[i]; 
			if(c[i] != i)	flag = 0;
		}
		if(n == m&&flag){
			cout << 1 <<endl;
			continue;
		}
		if(flag1 == -1){
			cout << 0 <<endl;
			continue;
		}
		for(long long i = 1; i < n; i++){
			if(!x[i]||x[i]&&!x[i+1]){
				ans = (ans*(v*v)%modd)%modd; 
			}
			else{
				ans = (ans*(v*(v-1)+1)%modd);
			}
		}
		cout << ans <<endl;
	}
	return 0;
}

