#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1000000007;
int n,m,T,v,c[1000005],d[1000005],f[1000005],ans;
int a[10000005],s[10000005];
int qp(int a,int b) {
	int res=1;
	while(b) {
		if(b&1) {
			res=(res*a)%mod;
		}
		a=(a*a)%mod;
		b>>=1;
	}
	return res%mod;
}
struct ys {
	int c,d;
} q[1000005];
bool cmp(ys x,ys y) {
	if(x.c==y.c) {
		return x.d<y.d;
	}
	return x.c<y.c;
}
bool fa=0,fb=0;
void dfs(int p){
	if(p==n+1){
		for(int i=2;i<=n;i++){
			if(a[i]){
				if(a[i]!=s[i]){
					return;
				}
				if(a[i-1]){
					if(s[i-1]!=a[i-1]){
						return;
					}
				}
			}
		}
		ans++;
		return;
	}
	for(int i=1;i<=v;i++){
		s[p]=i;
		dfs(p+1);
	}
}
void solve() {
	ans=0;
	cin>>n>>m>>v;
	for(int i=1; i<=m; i++) {
		cin>>q[i].c>>q[i].d;
		if(q[i].c!=i){
			fa=1;
		}
	}
	fb=1;
	if(!fa&&n==m) {
		cout<<qp(v*v-v+1,n-1)%mod<<endl;
		return;
	}
	if(!fb) {
		
	}
	sort(q+1,q+1+m,cmp);
	a[q[1].c]=q[1].d;
	for(int i=2; i<=m; i++) {
		a[q[i].c]=q[i].d;
		if(q[i].c==q[i-1].c) {
			if(q[i].d!=q[i-1].d) {
				cout<<0<<endl;
				return;
			}
		}
	}
	dfs(1);
	cout<<ans<<endl;
	return;
}
signed main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	while(T--) {
		solve();
	}
	return 0;
}
