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:
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<stack>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<int,P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)
#define sor(v) sort(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)
#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)
#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())
#define mp1(a,b,c) P1(a,P(b,c))

const int INF=1000000000;
const int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
const int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

vector<P> nullvP;

struct Graph{
	int V;
	vector<vector<P>> G;
	void init(int _V){
		V = _V;
		G.clear();
		rep(i,V+1){
			G.pb(nullvP);
		}
	}
	void add_edge(int a,int b,int w = 1){
		//cout << a << " " << b << " " << w << endl;
		G[a].pb(P(b,w));
		//G[b].pb(P(a,w));
	}
	int dijkstra(int s,int t){
		//cout << "(s,t)=" << s << "," << t << endl;
		bool used[52];
		int dist[52];
		priority_queue<P,vector<P>,greater<P>> que;
		rep(i,52){
			used[i] = false;
			dist[i] = INF;
		}
		dist[s] = 0;
		que.push(P(0,s));
		while(!que.empty()){
			P p = que.top(); que.pop();
			//cout << p.fr << " " << p.sc << endl;
			if(used[p.sc])continue;
			used[p.sc] = true;
			for(P ed: G[p.sc]){
				if(dist[ed.fr] > dist[p.sc]+ed.sc){
					dist[ed.fr] = dist[p.sc]+ed.sc;
					que.push(P(dist[ed.fr],ed.fr));
				}
			}
		}
		return dist[t];
	}
}G;

int main(){
	static int n,m,q;
	static string s,t;
	//static int a[200010],b[200010],c[200010],d[200010];
	scanf("%d%d%d",&n,&m,&q);
	cin >> s >> t;
	/*rep1(i,q){
		scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
	}*/
	
	static int lw[100010],rw[100010],le[100010],re[100010];
	static int ln[100010],rn[100010],ls[100010],rs[100010];
	lw[0] = le[0] = 0;
	rep1(i,n){
		lw[i] = lw[i-1];
		le[i] = le[i-1];
		if(s[i-1] == 'W')lw[i] = i;
		else le[i] = i;
	}
	ln[0] = ls[0] = 0;
	rep1(i,m){
		ln[i] = ln[i-1];
		ls[i] = ls[i-1];
		if(t[i-1] == 'N')ln[i] = i;
		else ls[i] = i;
	}
	rw[n+1] = re[n+1] = n+1;
	rrep1(i,n){
		rw[i] = rw[i+1];
		re[i] = re[i+1];
		if(s[i-1] == 'W')rw[i] = i;
		else re[i] = i;
	}
	rn[m+1] = rs[m+1] = m+1;
	rrep1(i,m){
		rn[i] = rn[i+1];
		rs[i] = rs[i+1];
		if(t[i-1] == 'N')rn[i] = i;
		else rs[i] = i;
	}
	
	//rep1(i,n)cout << lw[i] << " " << rw[i] << " " << le[i] << " " << re[i] << endl;
	//rep1(i,m)cout << ln[i] << " " << rn[i] << " " << ls[i] << " " << rs[i] << endl;
	
	rep1(loop,q){
		int a,b,c,d;
		scanf("%d%d%d%d",&a,&b,&c,&d);
		vector<int> X,Y;
		X.pb(a); X.pb(c);
		if(s[a-1] == 'E'){
			if(lw[a] != 0)X.pb(lw[a]);
			if(rw[a] != n+1)X.pb(rw[a]);
		}
		else {
			if(le[a] != 0)X.pb(le[a]);
			if(re[a] != n+1)X.pb(re[a]);
		}
		if(s[c-1] == 'E'){
			if(lw[c] != 0)X.pb(lw[c]);
			if(rw[c] != n+1)X.pb(rw[c]);
		}
		else {
			if(le[c] != 0)X.pb(le[c]);
			if(re[c] != n+1)X.pb(re[c]);
		}
		Y.pb(b); Y.pb(d);
		if(t[b-1] == 'S'){
			if(ln[b] != 0)Y.pb(ln[b]);
			if(rn[b] != m+1)Y.pb(rn[b]);
		}
		else {
			if(ls[b] != 0)Y.pb(ls[b]);
			if(rs[b] != m+1)Y.pb(rs[b]);
		}
		if(t[d-1] == 'S'){
			if(ln[d] != 0)Y.pb(ln[d]);
			if(rn[d] != m+1)Y.pb(rn[d]);
		}
		else {
			if(ls[d] != 0)Y.pb(ls[d]);
			if(rs[d] != m+1)Y.pb(rs[d]);
		}
		sor(X); uniq(X);
		sor(Y); uniq(Y);
		
		/*cout << "X:";
		for(int i: X)cout << i << " ";
		cout << endl;
		cout << "Y:";
		for(int i: Y)cout << i << " ";
		cout << endl;*/
		
		int _a = lower_bound(X.begin(),X.end(),a)-X.begin();
		int _b = lower_bound(Y.begin(),Y.end(),b)-Y.begin();
		int _c = lower_bound(X.begin(),X.end(),c)-X.begin();
		int _d = lower_bound(Y.begin(),Y.end(),d)-Y.begin();
		G.init(X.size()*Y.size());
		rep(i,X.size())rep(j,Y.size())for(int k = j+1 ; k < Y.size() ; k ++){
			if(s[X[i]-1] == 'W')G.add_edge(i*Y.size()+k,i*Y.size()+j,Y[k]-Y[j]);
			else G.add_edge(i*Y.size()+j,i*Y.size()+k,Y[k]-Y[j]);
		}
		rep(i,X.size())for(int j = i+1 ; j < X.size() ; j ++)rep(k,Y.size()){
			if(t[Y[k]-1] == 'N')G.add_edge(j*Y.size()+k,i*Y.size()+k,X[j]-X[i]);
			else G.add_edge(i*Y.size()+k,j*Y.size()+k,X[j]-X[i]);
		}
		int ret = G.dijkstra(_a*Y.size()+_b,_c*Y.size()+_d);
		if(ret >= INF)puts("-1");
		else printf("%d\n",ret);
		//printf("%d\n",G.dijkstra(_a*Y.size()+_b,_c*Y.size()+_d));
	}
}