#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e4+3,P=1e9+7;
int T,n,m,v,c,d,p[N],s,f[N][N];
bool h;
int dfs(int x,int y){
	if(x==n)return 1;
	int&s=f[x][y];
	if(~s)return s;
	s=0;
	int t;
	if(p[x+1])t=dfs(x+1,p[x+1]);
	else t=dfs(x+1,0);
	if(y){
		if(p[x+1])s=dfs(x+1,p[x+1]);
		else s=(ll)dfs(x+1,1)*v%P;
		return s=(s+(ll)t*(((ll)v*v-v)%P))%P;
	}
	return s=(ll)t*v%P*v%P;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d",&n,&m,&v);
		s=h=0;
		for(int i=1;i<=n;i++){
			p[i]=0;
			for(int j=0;j<=v;j++)f[i][j]=-1;
		}
		for(int i=1;i<=m;i++){
			scanf("%d%d",&c,&d);
			if(p[c]&&p[c]!=d)h=1;
			p[c]=d;
		}
		if(h){
			printf("0\n");
			continue;
		}
		cout<<dfs(1,p[1])<<'\n';
	}
}
