Problem: In Takaha-shi, the capital of Republic of AtCoder, there are N roads extending east and west, and M roads extending north and south. There are no other roads. The i-th east-west road from the north and the j-th north-south road from the west cross at the intersection (i, j). Two east-west roads do not cross, nor do two north-south roads. The distance between two adjacent roads in the same direction is 1.

Each road is one-way; one can only walk in one direction. The permitted direction for each road is described by a string S of length N and a string T of length M, as follows:

* If the i-th character in S is `W`, one can only walk westward along the i-th east-west road from the north;
* If the i-th character in S is `E`, one can only walk eastward along the i-th east-west road from the north;
* If the i-th character in T is `N`, one can only walk northward along the i-th north-south road from the west;
* If the i-th character in T is `S`, one can only walk southward along the i-th south-west road from the west.



Process the following Q queries:

* In the i-th query, a_i, b_i, c_i and d_i are given. What is the minimum distance to travel to reach the intersection (c_i, d_i) from the intersection (a_i, b_i) by walking along the roads?

Constraints

* 2 \leq N \leq 100000
* 2 \leq M \leq 100000
* 2 \leq Q \leq 200000
* |S| = N
* S consists of `W` and `E`.
* |T| = M
* T consists of `N` and `S`.
* 1 \leq a_i \leq N
* 1 \leq b_i \leq M
* 1 \leq c_i \leq N
* 1 \leq d_i \leq M
* (a_i, b_i) \neq (c_i, d_i)

Input

Input is given from Standard Input in the following format:


N M Q
S
T
a_1 b_1 c_1 d_1
a_2 b_2 c_2 d_2
:
a_Q b_Q c_Q d_Q


Output

In the i-th line, print the response to the i-th query. If the intersection (c_i, d_i) cannot be reached from the intersection (a_i, b_i) by walking along the roads, print `-1` instead.

Examples

Input

4 5 4
EEWW
NSNNS
4 1 1 4
1 3 1 2
4 2 3 2
3 3 3 5


Output

6
11
5
4


Input

3 3 2
EEE
SSS
1 1 3 3
3 3 1 1


Output

4
-1


Input

9 7 10
EEEEEWEWW
NSSSNSN
4 6 9 2
3 7 6 7
7 5 3 5
1 1 8 1
4 3 5 4
7 4 6 4
2 5 8 6
6 6 2 7
2 4 7 5
7 2 9 7


Output

9
-1
4
9
2
3
7
7
6
-1

Solution (in CPP):

Solution:
/* --- author: dxm --- */
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

#define INF 1e9
#define Inf 1e18
#define rep(i,n) for(int (i)=0;(i)<n;i++)
#define REP(i,n) for(int (i)=1;(i)<=n;i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;

void qread(int &x){
	int neg=1;x=0;
	char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')neg=-1;c=getchar();}
	while(c>='0'&&c<='9')x=10*x+c-'0',c=getchar();
	x*=neg;
}

const int maxn=200005;
int n,m,q,cntr,cntc;
char rs[maxn],cs[maxn];
int r[maxn],c[maxn],curr[maxn],curc[maxn];
int rnxt[maxn][2],rprv[maxn][2],cnxt[maxn][2],cprv[maxn][2];
int dist[maxn];
priority_queue<pii>pq;
vector<pii>edge[maxn];
//0: from left to right & from up to down
//1: from right to left & from down to up

int getid(int x,int y){
	return x*cntc+y;
}

int calc(int s,int t,int n){
	rep(i,n)dist[i]=INF;
	dist[s]=0;
	pq.push(mp(0,s));
	while(!pq.empty()){
		pii x=pq.top();
		pq.pop();
		x.fst=-x.fst;
		if(dist[x.snd]!=x.fst)continue;
		rep(i,edge[x.snd].size()){
			pii y=edge[x.snd][i];
			if(dist[y.fst]>dist[x.snd]+y.snd){
				dist[y.fst]=dist[x.snd]+y.snd;
				pq.push(mp(-dist[y.fst],y.fst));
			} 
		}
	}
	if(dist[t]==INF)return -1;
	return dist[t];
}

int main(){
#ifdef FILE
	freopen("FILE.in","r",stdin);
	freopen("FILE.out","w",stdout);
#endif
	scanf("%d%d%d%s%s",&n,&m,&q,rs,cs);
	rep(i,n){
		r[i]=rs[i]=='W';
		rep(j,2){
			rprv[i][j]=(!i?-1:rprv[i-1][j]);
			if(j==r[i])rprv[i][j]=i;
		} 
	}
	for(int i=n-1;i>=0;i--){
		rep(j,2){
			rnxt[i][j]=(i==n-1?n:rnxt[i+1][j]);
			if(j==r[i])rnxt[i][j]=i;
		}
	}
	rep(i,m){
		c[i]=cs[i]=='N';
		rep(j,2){
			cprv[i][j]=(!i?-1:cprv[i-1][j]);
			if(j==c[i])cprv[i][j]=i;
		}
	}
	for(int i=m-1;i>=0;i--){
		rep(j,2){
			cnxt[i][j]=(i==m-1?m:cnxt[i+1][j]);
			if(j==c[i])cnxt[i][j]=i;
		}
	}
	while(q--){
		int a,b,c,d;
		scanf("%d%d%d%d",&a,&b,&c,&d);
		a--;b--;c--;d--;
		cntr=0,cntc=0;
		int ida,idb,idc,idd;
		curr[cntr++]=a;
		if(a&&rprv[a-1][1^r[a]]>=0)curr[cntr++]=rprv[a-1][1^r[a]];
		if(a<n-1&&rnxt[a+1][1^r[a]]<n)curr[cntr++]=rnxt[a+1][1^r[a]];
		curr[cntr++]=c;
		if(c&&rprv[c-1][1^r[c]]>=0)curr[cntr++]=rprv[c-1][1^r[c]];
		if(c<n-1&&rnxt[c+1][1^r[c]]<n)curr[cntr++]=rnxt[c+1][1^r[c]];
		curc[cntc++]=b;
		if(b&&cprv[b-1][1^::c[b]]>=0)curc[cntc++]=cprv[b-1][1^::c[b]];
		if(b<m-1&&cnxt[b+1][1^::c[b]]<m)curc[cntc++]=cnxt[b+1][1^::c[b]];
		curc[cntc++]=d;
		if(d&&cprv[d-1][1^::c[d]]>=0)curc[cntc++]=cprv[d-1][1^::c[d]];
		if(d<m-1&&cnxt[d+1][1^::c[d]]<m)curc[cntc++]=cnxt[d+1][1^::c[d]];
		sort(curr,curr+cntr);
		sort(curc,curc+cntc);
		rep(i,cntr){
			if(i>0&&curr[i]==curr[i-1]){
				for(int j=i+1;j<cntr;j++)curr[j-1]=curr[j];
				cntr--;
			}
		}
		rep(i,cntr){
			if(curr[i]==a)ida=i;
			if(curr[i]==c)idc=i;
		}
		rep(i,cntc){
			if(i>0&&curc[i]==curc[i-1]){
				for(int j=i+1;j<cntc;j++)curc[j-1]=curc[j];
				cntc--;
			}
		}
		rep(i,cntc){
			if(curc[i]==b)idb=i;
			if(curc[i]==d)idd=i;
		}
		rep(i,cntr)rep(j,cntc){
			if(!r[curr[i]]&&j<cntc-1)edge[getid(i,j)].pb(mp(getid(i,j+1),curc[j+1]-curc[j]));
			if(r[curr[i]]&&j>0)edge[getid(i,j)].pb(mp(getid(i,j-1),curc[j]-curc[j-1]));
			if(!::c[curc[j]]&&i<cntr-1)edge[getid(i,j)].pb(mp(getid(i+1,j),curr[i+1]-curr[i]));
			if(::c[curc[j]]&&i>0)edge[getid(i,j)].pb(mp(getid(i-1,j),curr[i]-curr[i-1]));
		}
		printf("%d\n",calc(getid(ida,idb),getid(idc,idd),cntr*cntc));
		rep(i,cntr*cntc)edge[i].clear();
	}
#ifdef TIME
	printf("Running Time = %d ms\n",int(clock()*1000.0/CLOCKS_PER_SEC));
#endif
	return 0;
}

/*
Input:
-----------------
Output:
*/