#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int T,n,m,v;
int c[1000005],d[1000005],a[1000006];
int rulle[1000006];
int runum[1000006];
void init(){
	memset(rulle,0,sizeof rulle);
	memset(a,0,sizeof a);
	memset(d,0,sizeof d);
	memset(c,0,sizeof c); 
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--){
		int ans=0,allrulle=0; bool flag=1;
		scanf("%d %d %d",&n,&m,&v);
		init();
		for(int i=1;i<=v;++i)
			rulle[i]=v;
		
		for(int i=1;i<=m;++i){
			scanf("%d %d",&c[i],&d[i]);
			if(a[c[i]]!=d[i]&&a[c[i]]!=0) flag=0;
			else if(a[c[i]]==0){
				a[c[i]]=d[i];
				if(a[c[i]-1]!=0){
					if(rulle[a[c[i]-1]]==1&&runum[a[c[i]-1]]!=a[c[i]]) flag=0;
					else{
						rulle[a[c[i]-1]]=1;
						runum[a[c[i]-1]]=a[c[i]];
					}
						
				}
				if(a[c[i]+1]!=0){
					if(rulle[a[c[i]]]==1&&runum[a[c[i]]]!=a[c[i]+1]) flag=0;
					else{
						rulle[a[c[i]]]=1;
						runum[a[c[i]]]=a[c[i]+1];
					}
						
				}
			}
		}
		if(v==1){
			printf("1\n"); continue;
		}
		if(!flag){
			printf("0\n"); continue;
		}
		for(int i=1;i<=v;++i)
			allrulle+=rulle[i]%mod;
		ans=1;
		for(int i=1;i<=n-1;++i)
			ans*=allrulle%mod;
		printf("%d\n",ans%mod);
		
	}
} 
