#include"bits/stdc++.h"
#define MOD 1000000007
using namespace std;
int n,m,v;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;cin>>T;
	while(T--){
		cin>>n>>m>>v;
		long long ans = 1;
		bool flag = 0;
		vector<int> mrr(n+1,0);
		for(int i=1;i<=m;i++){
			int c,d;cin>>c>>d;
			if(mrr[c] and mrr[c]!=d)flag = 1;
			mrr[c]=d;
		}
		if(flag){
			printf("0\n");
			continue;	
		}
		for(int i=2;i<=n;i++){
			if(mrr[i] and mrr[i-1]){
				ans = (ans * ((v-1)*v+1)%MOD)%MOD;
			}else{
				ans = (ans * (v*v)%MOD)%MOD;
			}
		}
		printf("%lld\n",ans%MOD);
		continue;
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
