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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;

template <typename Tp> inline void getint(Tp &num){
	register int ch, neg = 0;
	while(!isdigit(ch = getchar())) if(ch == '-') neg = 1;
	num = ch & 15;
	while(isdigit(ch = getchar())) num = num * 10 + (ch & 15);
	if(neg) num = -num;
}

int N, M, Q, S, T, tope = 0; char s[100005], t[100005];
int UW[100005], UE[100005], DW[100005], DE[100005];
int LN[100005], LS[100005], RN[100005], RS[100005];
int R[10], C[10], topR, topC, id[10][10], cnt;
int dis[40], q[40], fr, re, inq[40];
struct Edge {int np, val; Edge *nxt;} E[65], *V[40];
#define tasu1(x) (x = x < 40 ? x + 1 : 0)

inline void addedge(const int &u, const int &v, const int &w){
	E[++tope].np = v, E[tope].val = w;
	E[tope].nxt = V[u], V[u] = E + tope;
	
//	printf("%d -> %d : %d\n", u, v, w);
}

inline int SPFA(){
	memset(dis, 0x3f, sizeof(int) * (cnt + 1));
	memset(inq, 0, sizeof(int) * (cnt + 1));
	dis[S] = 0, inq[q[0] = S] = 1, fr = 0, re = 1;
	while(fr != re){
		const int u = q[fr]; tasu1(fr), inq[u] = 0;
		for(register Edge *ne = V[u]; ne; ne = ne->nxt)
			if(dis[u] + ne->val < dis[ne->np]){
				dis[ne->np] = dis[u] + ne->val;
				if(!inq[ne->np]) q[re] = ne->np, tasu1(re), inq[ne->np] = 1;
			}
	}
	return dis[T] < inf ? dis[T] : -1;
}

int main(){
	getint(N), getint(M), getint(Q), scanf("%s%s", s + 1, t + 1);
	UW[0] = UE[0] = DW[N + 1] = DE[N + 1] = N + 1;
	LN[0] = LS[0] = RN[M + 1] = RS[M + 1] = M + 1;
	for(register int i = 1; i <= N; i++){
		UW[i] = s[i] == 'W' ? i : UW[i - 1];
		UE[i] = s[i] == 'E' ? i : UE[i - 1];
	}
	for(register int i = N; i >= 1; i--){
		DW[i] = s[i] == 'W' ? i : DW[i + 1];
		DE[i] = s[i] == 'E' ? i : DE[i + 1];
	}
	for(register int i = 1; i <= M; i++){
		LN[i] = t[i] == 'N' ? i : LN[i - 1];
		LS[i] = t[i] == 'S' ? i : LS[i - 1];
	}
	for(register int i = M; i >= 1; i--){
		RN[i] = t[i] == 'N' ? i : RN[i + 1];
		RS[i] = t[i] == 'S' ? i : RS[i + 1];
	}
	while(Q--){
		int a, b, c, d; getint(a), getint(b), getint(c), getint(d);
		R[1] = UW[a], R[2] = DW[a], R[3] = UW[c], R[4] = DW[c];
		R[5] = UE[a], R[6] = DE[a], R[7] = UE[c], R[8] = DE[c];
		C[1] = LN[b], C[2] = RN[b], C[3] = LN[d], C[4] = RN[d];
		C[5] = LS[b], C[6] = RS[b], C[7] = LS[d], C[8] = RS[d];
		sort(R + 1, R + 9), topR = unique(R + 1, R + 9) - R - 1;
		sort(C + 1, C + 9), topC = unique(C + 1, C + 9) - C - 1, cnt = 0;
		if(R[topR] > N) topR--; if(C[topC] > M) topC--;
		for(register int i = 1; i <= topR; i++)
			for(register int j = 1; j <= topC; j++){
				id[i][j] = ++cnt;
				if(R[i] == a && C[j] == b) S = cnt;
				if(R[i] == c && C[j] == d) T = cnt;
			}
		
//		puts("\n======= Intersect Debug hajimaru ======");
//		for(register int i = 1; i <= topR; i++) printf("(%d, %c) ", R[i], s[R[i]]); puts("");
//		for(register int i = 1; i <= topC; i++) printf("(%d, %c) ", C[i], t[C[i]]); puts("");
//		puts("========= Intersect Debug owaru =======\n");
//		
//		puts("\n========== ID Debug hajimaru ==========");
//		for(register int i = 1; i <= topR; i++, puts(""))
//			for(register int j = 1; j <= topC; j++)
//				printf("[%d](%d, %d) ", id[i][j], R[i], C[j]);
//		puts("=========== ID Debug owaru ===========\n");
//		
//		
//		puts("\n========== Edge Debug hajimaru =========");
		
		memset(V, 0, sizeof(Edge*) * (cnt + 1)), tope = 0;
		for(register int i = 1; i <= topR; i++)
			for(register int j = 1; j < topC; j++)
				if(s[R[i]] == 'W') addedge(id[i][j + 1], id[i][j], C[j + 1] - C[j]);
				else addedge(id[i][j], id[i][j + 1], C[j + 1] - C[j]);
		for(register int j = 1; j <= topC; j++)
			for(register int i = 1; i < topR; i++)
				if(t[C[j]] == 'N') addedge(id[i + 1][j], id[i][j], R[i + 1] - R[i]);
				else addedge(id[i][j], id[i + 1][j], R[i + 1] - R[i]);
		
//		puts("============ Edge Debug owaru ==========\n");
		
		printf("%d\n", SPFA());
	}
	return 0;
}