#include<bits/stdc++.h>
typedef long long i64;
const int P=1e9+7;
int T,h,w,m,n;
int xs[33],ys[33],xp,yp,vs[33],vp,ts[33];
int rc[33][5],mv[33][33],as[33][33];
void mins(int&a,int b){if(a>b)a=b;}
int pw(int a,int n){
int v=1;
for(;n;n>>=1,a=i64(a)*a%P)if(n&1)v=i64(v)*a%P;
return v;
}
int main(){
for(scanf("%d",&T);T;--T){
int ans=0;
scanf("%d%d%d%d",&h,&w,&m,&n);
xp=yp=vp=0;
xs[xp++]=1;
xs[xp++]=h+1;
ys[yp++]=1;
ys[yp++]=w+1;
vs[vp++]=m;
for(int i=0;i<n;++i){
for(int j=0;j<5;++j)scanf("%d",rc[i]+j);
xs[xp++]=rc[i][0];
xs[xp++]=rc[i][2]+1;
ys[yp++]=rc[i][1];
ys[yp++]=rc[i][3]+1;
vs[vp++]=rc[i][4];
vs[vp++]=rc[i][4]-1;
}
std::sort(xs,xs+xp);
xp=std::unique(xs,xs+xp)-xs-1;
std::sort(ys,ys+yp);
yp=std::unique(ys,ys+yp)-ys-1;
std::sort(vs,vs+vp);
vp=std::unique(vs,vs+vp)-vs;
for(int i=0;i<xp;++i)
for(int j=0;j<yp;++j)as[i][j]=(xs[i+1]-xs[i])*(ys[j+1]-ys[j]);
for(int t=0;t<n;++t){
rc[t][0]=std::lower_bound(xs,xs+xp,rc[t][0])-xs;
rc[t][2]=std::lower_bound(xs,xs+xp,rc[t][2]+1)-xs;
rc[t][1]=std::lower_bound(ys,ys+yp,rc[t][1])-ys;
rc[t][3]=std::lower_bound(ys,ys+yp,rc[t][3]+1)-ys;
rc[t][4]=std::lower_bound(vs,vs+vp,rc[t][4])-vs;
}
for(int S=0;S<(1<<n);++S){
for(int i=0;i<xp;++i)
for(int j=0;j<yp;++j)mv[i][j]=vp-1;
int s=1;
for(int t=0;t<n;++t){
int v=rc[t][4];
if(S>>t&1)s=-s,--v;
for(int i=rc[t][0];i<rc[t][2];++i)
for(int j=rc[t][1];j<rc[t][3];++j)mins(mv[i][j],v);
}
for(int i=0;i<vp;++i)ts[i]=0;
for(int i=0;i<xp;++i)
for(int j=0;j<yp;++j)ts[mv[i][j]]+=as[i][j];
for(int i=0;i<vp;++i)s=i64(s)*pw(vs[i],ts[i])%P;
ans=(ans+s)%P;
}
printf("%d\n",(ans+P)%P);
}
return 0;
}