#include <bits/stdc++.h>
#define pb push_back
#define sqr(x) (x)*(x)
#define sz(a) int(a.size())
#define reset(a,b) memset(a,b,sizeof(a))
#define oo 1000000007
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int maxn=5007;
int T;
int V,H,VG[maxn],HG[maxn],K;
int lN[maxn],lS[maxn],rN[maxn],rS[maxn],llN[maxn],llS[maxn];
int tW[maxn],tE[maxn],bW[maxn],bE[maxn],bbW[maxn],bbE[maxn];
char VD[maxn],HD[maxn];
struct DijkstraMachine{
int x1,y1,x2,y2;
vector<pii> Vline,Hline;
map<pii,int> id;
int d[222],N,start,finish;
vector<pii> e[222];
bool free1[222];
void init(int a, int b, int c, int d){
x1=a; y1=b; x2=c; y2=d;
Vline.clear(); Hline.clear();
}
void addVline(int i){
if(i==-1) return;
int dir;
if(VD[i]=='N') dir=1; else dir=-1;
Vline.pb(pii(VG[i],dir));
}
void addHline(int i){
if(i==-1) return;
int dir;
if(HD[i]=='E') dir=1; else dir=-1;
Hline.pb(pii(HG[i],dir));
}
void addLineWithPoint(int x, int y){
int xi=lower_bound(VG+1,VG+V+1,x)-VG;
if(xi>V) xi=V;
int yi=lower_bound(HG+1,HG+H+1,y)-HG;
if(yi>H) yi=H;
addVline(llN[xi]);
addVline(llS[xi]);
addVline(lN[xi]);
addVline(lS[xi]);
addVline(rN[xi]);
addVline(rS[xi]);
addHline(bbE[yi]);
addHline(bbW[yi]);
addHline(tE[yi]);
addHline(tW[yi]);
addHline(bE[yi]);
addHline(bW[yi]);
}
void normalize(){
sort(Vline.begin(),Vline.end());
Vline.resize(unique(Vline.begin(),Vline.end())-Vline.begin());
sort(Hline.begin(),Hline.end());
Hline.resize(unique(Hline.begin(),Hline.end())-Hline.begin());
}
int getid(int x, int y){
int v=id[pii(x,y)];
if(v) return v;
id[pii(x,y)]=++N;
e[N].clear();
return N;
}
int calc(){
//init the graph
id.clear(); N=0;
start=getid(x1,y1);
finish=getid(x2,y2);
for(int i=0; i<sz(Vline); ++i)
for(int j=0; j<sz(Hline); ++j){
int xx=Vline[i].first, yy=Hline[j].first;
int u=getid(xx,yy);
for(int i2=0; i2<sz(Vline); ++i2){
int xx2=Vline[i2].first;
int v=getid(xx2,yy);
if(v==u) continue;
if( (xx2 - xx) * Hline[j].second > 0 )
e[u].pb(pii(v,abs(xx2-xx)));
}
for(int j2=0; j2<sz(Hline); ++j2){
int yy2=Hline[j2].first;
int v=getid(xx,yy2);
if(v==u) continue;
if( (yy2 - yy) * Vline[i].second > 0 )
e[u].pb(pii(v,abs(yy2-yy)));
}
if(x1==xx && yy!=y1 && (yy-y1)*Vline[i].second>0)
e[start].pb(pii(u,abs(yy-y1)));
if(x1!=xx && yy==y1 && (xx-x1)*Hline[j].second>0)
e[start].pb(pii(u,abs(xx-x1)));
if(x2==xx && yy!=y2 && (y2-yy)*Vline[i].second>0)
e[u].pb(pii(finish,abs(y2-yy)));
if(x2!=xx && yy==y2 && (x2-xx)*Hline[j].second>0)
e[u].pb(pii(finish,abs(x2-xx)));
}
for(int i=0; i<sz(Vline); ++i)
if(x1 == x2 && x1==Vline[i].first && (y2-y1)*Vline[i].second>0)
e[start].pb(pii(finish,abs(y2-y1)));
for(int j=0; j<sz(Hline); ++j)
if(y1 == y2 && y1==Hline[j].first && (x2-x1)*Hline[j].second>0)
e[start].pb(pii(finish,abs(x2-x1)));
// do dijktra
for(int u=1; u<=N; ++u) d[u]=oo, free1[u]=1;
d[start]=0;
while(1){
int u=-1, minv=oo;
for(int v=1; v<=N; ++v)
if(free1[v] && minv>d[v]){
minv=d[v];
u=v;
}
if(u==-1 || u==finish) break;
free1[u]=0;
for(int i=0; i<sz(e[u]); ++i){
int v=e[u][i].first, w=e[u][i].second;
if(free1[v] && d[v]>d[u]+w)
d[v]=d[u]+w;
}
}
if(d[finish]==oo) d[finish]=-1;
return d[finish];
}
}dijk;
int main(){
//freopen("input.txt","r",stdin);
scanf("%d",&T);
for(int tt=1; tt<=T; ++tt){
scanf("%d%d%d",&V,&H,&K);
VG[1]=0;
int v;
for(int i=2; i<=V; ++i){
scanf("%d",&v);
VG[i]=VG[i-1]+v;
}
HG[1]=0;
for(int i=2; i<=H; ++i){
scanf("%d",&v);
HG[i]=HG[i-1]+v;
}
scanf("%s",VD+1); scanf("%s",HD+1);
int pN=-1, pS=-1;
for(int i=1; i<=V; ++i){
llN[i]=pN; llS[i]=pS;
if(VD[i]=='N') pN=i;
else pS=i;
lN[i]=pN; lS[i]=pS;
}
pN=-1; pS=-1;
for(int i=V; i>=1; --i){
rN[i]=pN; rS[i]=pS;
if(VD[i]=='N') pN=i;
else pS=i;
}
int pE=-1, pW=-1;
for(int i=1; i<=H; ++i){
bbW[i]=pW; bbE[i]=pE;
if(HD[i]=='E') pE=i;
else pW=i;
bW[i]=pW; bE[i]=pE;
}
pE=-1; pW=-1;
for(int i=H; i>=1; --i){
tW[i]=pW; tE[i]=pE;
if(HD[i]=='E') pE=i;
else pW=i;
}
// solve
int x1,y1,x2,y2;
for(int it=0; it<K; ++it){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dijk.init(x1,y1,x2,y2);
dijk.addLineWithPoint(x1,y1);
dijk.addLineWithPoint(x2,y2);
dijk.normalize();
printf("%d\n",dijk.calc());
}
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgc3FyKHgpICh4KSooeCkKI2RlZmluZSBzeihhKSBpbnQoYS5zaXplKCkpCiNkZWZpbmUgcmVzZXQoYSxiKSBtZW1zZXQoYSxiLHNpemVvZihhKSkKI2RlZmluZSBvbyAxMDAwMDAwMDA3Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdHlwZWRlZiBwYWlyPGludCxpbnQ+IHBpaTsKdHlwZWRlZiBsb25nIGxvbmcgbGw7CgoKY29uc3QgaW50IG1heG49NTAwNzsKCmludCBUOwppbnQgVixILFZHW21heG5dLEhHW21heG5dLEs7CmludCBsTlttYXhuXSxsU1ttYX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Y7CiAgICAgICAgaW50IHlpPWxvd2VyX2JvdW5kKEhHKzEsSEcrSCsxLHkpLUhHOwogICAgICAgIGlmKHlpPkgpIHlpPUg7CiAgICAgICAgYWRkVmxpbmUobGxOW3hpXSk7CiAgICAgICAgYWRkVmxpbmUobGxTW3hpXSk7CiAgICAgICAgYWRkVmxpbmUobE5beGldKTsKICAgICAgICBhZGRWbGluZShsU1t4aV0pOwogICAgICAgIGFkZFZsaW5lKHJOW3hpXSk7CiAgICAgICAgYWRkVmxpbmUoclNbeGldKTsKICAgICAgICBhZGRIbGluZShiYkVbeWldKTsKICAgICAgICBhZGRIbGluZShiYldbeWldKTsKICAgICAgICBhZGRIbGluZSh0RVt5aV0pOwogICAgICAgIGFkZEhsaW5lKHRXW3lpXSk7CiAgICAgICAgYWRkSGxpbmUoYkVbeWldKTsKICAgICAgICBhZGRIbGluZShiV1t5aV0pOwogICAgfQoKICAgIHZvaWQgbm9ybWFsaXplKCl7CiAgICAgICAgc29ydChWbGluZS5iZWdpbigpLFZsaW5lLmVuZCgpKTsKICAgICAgICBWbGluZS5yZXNpemUodW5pcXVlKFZsaW5lLmJlZ2luKCksVmxpbmUuZW5kKCkpLVZsaW5lLmJlZ2luKCkpOwogICAgICAgIHNvcnQoSGxpbmUuYmVnaW4oKSxIbGluZS5lbmQoKSk7CiAgICAgICAgSGxpbmUucmVzaXplKHVuaXF1ZShIbGluZS5iZWdpbigpLEhsaW5lLmVuZCgpKS1IbGluZS5iZWdpbigpKTsKICAgIH0KCiAgICBpbnQgZ2V0aWQoaW50IHgsIGludCB5KXsKICAgICAgICBpbnQgdj1pZFtwaWkoeCx5KV07CiAgICAgICAgaWYodikgcmV0dXJuIHY7CiAgICAgICAgaWRbcGlpKHgseSldPSsrTjsKICAgICAgICBlW05dLmNsZWFyKCk7CiAgICAgICAgcmV0dXJuIE47CiAgICB9CgogICAgaW50IGNhbGMoKXsKICAgICAgICAvL2luaXQgdGhlIGdyYXBoCiAgICAgICAgaWQuY2xlYXIoKTsgTj0wOwogICAgICAgIHN0YXJ0PWdldGlkKHgxLHkxKTsKICAgICAgICBmaW5pc2g9Z2V0aWQoeDIseTIpOwogICAgICAgIGZvcihpbnQgaT0wOyBpPHN6KFZsaW5lKTsgKytpKQogICAgICAgICAgICBmb3IoaW50IGo9MDsgajxzeihIbGluZSk7ICsrail7CiAgICAgICAgICAgICAgICBpbnQgeHg9VmxpbmVbaV0uZmlyc3QsIHl5PUhsaW5lW2pdLmZpcnN0OwogICAgICAgICAgICAgICAgaW50IHU9Z2V0aWQoeHgseXkpOwoKICAgICAgICAgICAgICAgIGZvcihpbnQgaTI9MDsgaTI8c3ooVmxpbmUpOyArK2kyKXsKICAgICAgICAgICAgICAgICAgICBpbnQgeHgyPVZsaW5lW2kyXS5maXJzdDsKICAgICAgICAgICAgICAgICAgICBpbnQgdj1nZXRpZCh4eDIseXkpOwogICAgICAgICAgICAgICAgICAgIGlmKHY9PXUpIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgICAgIGlmKCAoeHgyIC0geHgpICogSGxpbmVbal0uc2Vjb25kID4gMCApCiAgICAgICAgICAgICAgICAgICAgICAgIGVbdV0ucGIocGlpKHYsYWJzKHh4Mi14eCkpKTsKICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICBmb3IoaW50IGoyPTA7IGoyPHN6KEhsaW5lKTsgKytqMil7CiAgICAgICAgICAgICAgICAgICAgaW50IHl5Mj1IbGluZVtqMl0uZmlyc3Q7CiAgICAgICAgICAgICAgICAgICAgaW50IHY9Z2V0aWQoeHgseXkyKTsKICAgICAgICAgICAgICAgICAgICBpZih2PT11KSBjb250aW51ZTsKICAgICAgICAgICAgICAgICAgICBpZiggKHl5MiAtIHl5KSAqIFZsaW5lW2ldLnNlY29uZCA+IDAgKQogICAgICAgICAgICAgICAgICAgICAgICBlW3VdLnBiKHBpaSh2LGFicyh5eTIteXkpKSk7CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgaWYoeDE9PXh4ICYmIHl5IT15MSAmJiAoeXkteTEpKlZsaW5lW2ldLnNlY29uZD4wKQogICAgICAgICAgICAgICAgICAgIGVbc3RhcnRdLnBiKHBpaSh1LGFicyh5eS15MSkpKTsKCiAgICAgICAgICAgICAgICBpZih4MSE9eHggJiYgeXk9PXkxICYmICh4eC14MSkqSGxpbmVbal0uc2Vjb25kPjApCiAgICAgICAgICAgICAgICAgICAgZVtzdGFydF0ucGIocGlpKHUsYWJzKHh4LXgxKSkpOwoKICAgICAgICAgICAgICAgIGlmKHgyPT14eCAmJiB5eSE9eTIgJiYgKHkyLXl5KSpWbGluZVtpXS5zZWNvbmQ+MCkKICAgICAgICAgICAgICAgICAgICBlW3VdLnBiKHBpaShmaW5pc2gsYWJzKHkyLXl5KSkpOwoKICAgICAgICAgICAgICAgIGlmKHgyIT14eCAmJiB5eT09eTIgJiYgKHgyLXh4KSpIbGluZVtqXS5zZWNvbmQ+MCkKICAgICAgICAgICAgICAgICAgICBlW3VdLnBiKHBpaShmaW5pc2gsYWJzKHgyLXh4KSkpOwoKICAgICAgICAgICAgfQoKICAgICAgICBmb3IoaW50IGk9MDsgaTxzeihWbGluZSk7ICsraSkKICAgICAgICAgICAgaWYoeDEgPT0geDIgJiYgeDE9PVZsaW5lW2ldLmZpcnN0ICYmICh5Mi15MSkqVmxpbmVbaV0uc2Vjb25kPjApCiAgICAgICAgICAgICAgICBlW3N0YXJ0XS5wYihwaWkoZmluaXNoLGFicyh5Mi15MSkpKTsKICAgICAgICBmb3IoaW50IGo9MDsgajxzeihIbGluZSk7ICsraikKICAgICAgICAgICAgaWYoeTEgPT0geTIgJiYgeTE9PUhsaW5lW2pdLmZpcnN0ICYmICh4Mi14MSkqSGxpbmVbal0uc2Vjb25kPjApCiAgICAgICAgICAgICAgICBlW3N0YXJ0XS5wYihwaWkoZmluaXNoLGFicyh4Mi14MSkpKTsKCiAgICAgICAgLy8gZG8gZGlqa3RyYQogICAgICAgIGZvcihpbnQgdT0xOyB1PD1OOyArK3UpIGRbdV09b28sIGZyZWUxW3VdPTE7CiAgICAgICAgZFtzdGFydF09MDsKICAgICAgICB3aGlsZSgxKXsKICAgICAgICAgICAgaW50IHU9LTEsIG1pbnY9b287CiAgICAgICAgICAgIGZvcihpbnQgdj0xOyB2PD1OOyArK3YpCiAgICAgICAgICAgICAgICBpZihmcmVlMVt2XSAmJiBtaW52PmRbdl0pewogICAgICAgICAgICAgICAgICAgIG1pbnY9ZFt2XTsKICAgICAgICAgICAgICAgICAgICB1PXY7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIGlmKHU9PS0xIHx8IHU9PWZpbmlzaCkgYnJlYWs7CiAgICAgICAgICAgIGZyZWUxW3VdPTA7CiAgICAgICAgICAgIGZvcihpbnQgaT0wOyBpPHN6KGVbdV0pOyArK2kpewogICAgICAgICAgICAgICAgaW50IHY9ZVt1XVtpXS5maXJzdCwgdz1lW3VdW2ldLnNlY29uZDsKICAgICAgICAgICAgICAgIGlmKGZyZWUxW3ZdICYmIGRbdl0+ZFt1XSt3KQogICAgICAgICAgICAgICAgICAgIGRbdl09ZFt1XSt3OwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmKGRbZmluaXNoXT09b28pIGRbZmluaXNoXT0tMTsKICAgICAgICByZXR1cm4gZFtmaW5pc2hdOwogICAgfQoKfWRpams7CgppbnQgbWFpbigpewogICAgLy9mcmVvcGVuKCJpbnB1dC50eHQiLCJyIixzdGRpbik7CiAgICBzY2FuZigiJWQiLCZUKTsKICAgIGZvcihpbnQgdHQ9MTsgdHQ8PVQ7ICsrdHQpewogICAgICAgIHNjYW5mKCIlZCVkJWQiLCZWLCZILCZLKTsKICAgICAgICBWR1sxXT0wOwogICAgICAgIGludCB2OwogICAgICAgIGZvcihpbnQgaT0yOyBpPD1WOyArK2kpewogICAgICAgICAgICBzY2FuZigiJWQiLCZ2KTsKICAgICAgICAgICAgVkdbaV09VkdbaS0xXSt2OwogICAgICAgIH0KICAgICAgICBIR1sxXT0wOwogICAgICAgIGZvcihpbnQgaT0yOyBpPD1IOyArK2kpewogICAgICAgICAgICBzY2FuZigiJWQiLCZ2KTsKICAgICAgICAgICAgSEdbaV09SEdbaS0xXSt2OwogICAgICAgIH0KICAgICAgICBzY2FuZigiJXMiLFZEKzEpOyBzY2FuZigiJXMiLEhEKzEpOwoKICAgICAgICBpbnQgcE49LTEsIHBTPS0xOwogICAgICAgIGZvcihpbnQgaT0xOyBpPD1WOyArK2kpewogICAgICAgICAgICBsbE5baV09cE47IGxsU1tpXT1wUzsKICAgICAgICAgICAgaWYoVkRbaV09PSdOJykgcE49aTsKICAgICAgICAgICAgZWxzZSBwUz1pOwogICAgICAgICAgICBsTltpXT1wTjsgbFNbaV09cFM7CiAgICAgICAgfQogICAgICAgIHBOPS0xOyBwUz0tMTsKICAgICAgICBmb3IoaW50IGk9VjsgaT49MTsgLS1pKXsKICAgICAgICAgICAgck5baV09cE47IHJTW2ldPXBTOwogICAgICAgICAgICBpZihWRFtpXT09J04nKSBwTj1pOwogICAgICAgICAgICBlbHNlIHBTPWk7CiAgICAgICAgfQoKICAgICAgICBpbnQgcEU9LTEsIHBXPS0xOwogICAgICAgIGZvcihpbnQgaT0xOyBpPD1IOyArK2kpewogICAgICAgICAgICBiYldbaV09cFc7IGJiRVtpXT1wRTsKICAgICAgICAgICAgaWYoSERbaV09PSdFJykgcEU9aTsKICAgICAgICAgICAgZWxzZSBwVz1pOwogICAgICAgICAgICBiV1tpXT1wVzsgYkVbaV09cEU7CiAgICAgICAgfQogICAgICAgIHBFPS0xOyBwVz0tMTsKICAgICAgICBmb3IoaW50IGk9SDsgaT49MTsgLS1pKXsKICAgICAgICAgICAgdFdbaV09cFc7IHRFW2ldPXBFOwogICAgICAgICAgICBpZihIRFtpXT09J0UnKSBwRT1pOwogICAgICAgICAgICBlbHNlIHBXPWk7CiAgICAgICAgfQoKICAgICAgICAvLyBzb2x2ZQoKICAgICAgICBpbnQgeDEseTEseDIseTI7CiAgICAgICAgZm9yKGludCBpdD0wOyBpdDxLOyArK2l0KXsKICAgICAgICAgICAgc2NhbmYoIiVkJWQlZCVkIiwmeDEsJnkxLCZ4MiwmeTIpOwogICAgICAgICAgICBkaWprLmluaXQoeDEseTEseDIseTIpOwogICAgICAgICAgICBkaWprLmFkZExpbmVXaXRoUG9pbnQoeDEseTEpOwogICAgICAgICAgICBkaWprLmFkZExpbmVXaXRoUG9pbnQoeDIseTIpOwogICAgICAgICAgICBkaWprLm5vcm1hbGl6ZSgpOwogICAgICAgICAgICBwcmludGYoIiVkXG4iLGRpamsuY2FsYygpKTsKICAgICAgICB9CiAgICB9Cn0=