#include<cstdio>
#include<algorithm>
const int N=50007;
typedef long long ll;
int xs[N*2],ys[N*2],xc,yc;
int cl[N],cr[N],cd[N],cu[N];
struct TR0
{
static const int M=N*20*8;
int tr[M],sm[M],ls[M],rs[M],rt[N*2],vc,nw;inline void ref(){nw=vc;}
int cpy(int x){int p=++vc;tr[p]=tr[x];sm[p]=sm[x];ls[p]=ls[x];rs[p]=rs[x];return p;}
inline void pull(int x,int n){sm[x]=(tr[x]?n:sm[ls[x]]+sm[rs[x]]);}
void upd(int&k,int L,int R,int v,int l,int r)
{
if(L>R)return;if(k<=nw)k=cpy(k);
if(L<=l&&r<=R){tr[k]+=v;pull(k,ys[r+1]-ys[l]);return;}
int mid=(l+r)>>1;
if(L<=mid)upd(ls[k],L,R,v,l,mid);
if(R>mid)upd(rs[k],L,R,v,mid+1,r);
pull(k,ys[r+1]-ys[l]);
}
int qry(int k,int L,int R,int l,int r)
{
if(L>R)return 0;if(!k||(L<=l&&r<=R))return sm[k];
if(tr[k])return ys[std::min(R,r)+1]-ys[std::max(L,l)];
int mid=(l+r)>>1,ans=0;
if(L<=mid)ans=qry(ls[k],L,R,l,mid);
if(R>mid)ans+=qry(rs[k],L,R,mid+1,r);
return ans;
}
};
struct TR
{
static const int M=N*20*8;
int tr[M],sm[M],ls[M],rs[M],rt[N*2],vc,nw;
ll hs[M];int ml[M],ad[M];inline void ref(){nw=vc;}
int cpy(int x)
{
int p=++vc;tr[p]=tr[x];sm[p]=sm[x];ls[p]=ls[x];rs[p]=rs[x];
hs[p]=hs[x];ml[p]=ml[x];ad[p]=ad[x];return p;
}
void adds(int&k,int ms,int as,int n)
{
if(k<=nw)k=cpy(k);hs[k]+=(ll)ms*sm[k]+(ll)as*n;
ad[k]+=as;(tr[k]?ad[k]:ml[k])+=ms;
}
void push(int k,int ln,int rn){if(ad[k]||ml[k])adds(ls[k],ml[k],ad[k],ln),adds(rs[k],ml[k],ad[k],rn),ml[k]=ad[k]=0;}
void pull(int x,int n){sm[x]=(tr[x]?n:sm[ls[x]]+sm[rs[x]]);hs[x]=hs[ls[x]]+hs[rs[x]];}
void upd(int&k,int L,int R,int v,int l,int r)
{
if(L>R)return;if(k<=nw)k=cpy(k);
if(L<=l&&r<=R){tr[k]+=v;sm[k]=(tr[k]?xs[r+1]-xs[l]:sm[ls[k]]+sm[rs[k]]);return;}
int mid=(l+r)>>1;push(k,xs[mid+1]-xs[l],xs[r+1]-xs[mid+1]);
if(L<=mid)upd(ls[k],L,R,v,l,mid);
if(R>mid)upd(rs[k],L,R,v,mid+1,r);
pull(k,xs[r+1]-xs[l]);
}
int qry(int k,int L,int R,int l,int r)
{
if(L>R)return 0;if(!k||(L<=l&&r<=R))return sm[k];
if(tr[k])return xs[std::min(R,r)+1]-xs[std::max(L,l)];
int mid=(l+r)>>1,ans=0;
if(L<=mid)ans=qry(ls[k],L,R,l,mid);
if(R>mid)ans+=qry(rs[k],L,R,mid+1,r);
return ans;
}
ll hsqry(int k,int L,int R,int l,int r)
{
if(L>R)return 0;if(k<=nw)k=cpy(k);
if(!k||(L<=l&&r<=R))return hs[k];
int mid=(l+r)>>1;ll ans=0;push(k,xs[mid+1]-xs[l],xs[r+1]-xs[mid+1]);
if(L<=mid)ans=hsqry(ls[k],L,R,l,mid);
if(R>mid)ans+=hsqry(rs[k],L,R,mid+1,r);
return ans;
}
};
TR0 T0;TR T1;
struct pii{int l,r,v;};
std::vector<pii> xv[N*2],yv[N*2];
int T0q(int t,int qd,int qu,int td,int tu)
{
int A=(T0.qry(T0.rt[t],td,td,1,yc-1)?1:0)*(tu==td?qu-qd+1:ys[td+1]-qd);
if(tu==td)return A;A+=(T0.qry(T0.rt[t],tu,tu,1,yc-1)?1:0)*(qu-ys[tu]+1);
return A+T0.qry(T0.rt[t],td+1,tu-1,1,yc-1);
}
int main()
{
int r,c,n,q,m;scanf("%d%d%d%d%d",&r,&c,&n,&q,&m);
xs[++xc]=0;xs[++xc]=r;ys[++yc]=0;ys[++yc]=c;
for(int i=1;i<=n;++i)
{
scanf("%d%d%d%d",cl+i,cd+i,cr+i,cu+i);
if(cd[i]>cu[i])std::swap(cd[i],cu[i]);
if(cl[i]>cr[i])std::swap(cl[i],cr[i]);
xs[++xc]=cl[i];xs[++xc]=cr[i];
ys[++yc]=cd[i];ys[++yc]=cu[i];
}
std::sort(xs+1,xs+xc+1);xc=std::unique(xs+1,xs+xc+1)-xs-1;
std::sort(ys+1,ys+yc+1);yc=std::unique(ys+1,ys+yc+1)-ys-1;
for(int i=1;i<=n;++i)
{
cl[i]=std::lower_bound(xs+1,xs+xc+1,cl[i])-xs;
cr[i]=std::lower_bound(xs+1,xs+xc+1,cr[i])-xs;
cd[i]=std::lower_bound(ys+1,ys+yc+1,cd[i])-ys;
cu[i]=std::lower_bound(ys+1,ys+yc+1,cu[i])-ys;
xv[cl[i]].push_back({cd[i],cu[i]-1,1});
xv[cr[i]].push_back({cd[i],cu[i]-1,-1});
yv[cd[i]].push_back({cl[i],cr[i]-1,1});
yv[cu[i]].push_back({cl[i],cr[i]-1,-1});
}
for(int i=1;i<xc;++i){T0.rt[i]=T0.rt[i-1];for(pii z:xv[i])T0.upd(T0.rt[i],z.l,z.r,z.v,1,yc-1);T0.ref();}
for(int i=1;i<yc;++i){T1.rt[i]=T1.rt[i-1];for(pii z:yv[i])T1.upd(T1.rt[i],z.l,z.r,z.v,1,xc-1);T1.adds(T1.rt[i],ys[i+1]-ys[i],0,r);T1.ref();}
ll lsa=0;
while(q--)
{
int ql,qr,qu,qd,v;scanf("%d%d%d%d%d",&ql,&qd,&qr,&qu,&v);
ql=(ql+(__int128)lsa*v)%m;qr=(qr+(__int128)lsa*v)%m;
qd=(qd+(__int128)lsa*v)%m;qu=(qu+(__int128)lsa*v)%m;
if(qd>qu)std::swap(qd,qu);if(ql>qr)std::swap(ql,qr);--qr;--qu;
if(qu<qd||qr<ql){printf("%lld\n",lsa=0);continue;}
int tl=std::upper_bound(xs+1,xs+xc+1,ql)-xs-1;
int tr=std::upper_bound(xs+1,xs+xc+1,qr)-xs-1;
int td=std::upper_bound(ys+1,ys+yc+1,qd)-ys-1;
int tu=std::upper_bound(ys+1,ys+yc+1,qu)-ys-1;
ll ans=(ll)T0q(tl,qd,qu,td,tu)*(tl==tr?qr-ql+1:xs[tl+1]-ql);
if(tl==tr)goto prt;ans+=(ll)T0q(tr,qd,qu,td,tu)*(qr-xs[tr]+1);
if(tu==td){ans+=(ll)T1.qry(T1.rt[td],tl+1,tr-1,1,xc-1)*(qu-qd+1);goto prt;}
ans+=(ll)T1.qry(T1.rt[tu],tl+1,tr-1,1,xc-1)*(qu-ys[tu]+1)+(ll)T1.qry(T1.rt[td],tl+1,tr-1,1,xc-1)*(ys[td+1]-qd);
{int t=T1.vc;ans+=T1.hsqry(T1.rt[tu-1],tl+1,tr-1,1,xc-1);ans-=T1.hsqry(T1.rt[td],tl+1,tr-1,1,xc-1);T1.vc=t;}
prt:printf("%lld\n",lsa=ans);
}
return 0;
}