spoj#DCEPCA02. Ant Colony Optimization

Ant Colony Optimization

Ants are known to move in groups and in perfect order. However, in our problem the ants can move only in directions Left(L), Right(R), Up(U), Down(D). The place is a rectangular grid. There lives 2 types of ants : Type A and Type B. Each ant type has its own queen ant which defines the 2 directions at which their type of ants can reproduce ants.

A starting cell is given where one ant of each type is standing (A and B). When the process begins, each type of ant produces 4 ants, 2 ants of their type in the 2 directions (1 ant per direction) specified by their queen ant and 2 ants of the other type in the remaining 2 directions. So if queen ant of type A says LR and queen ant of type B says UD then A ant produces new type A ants at left cell and right cell and new type B ants at up cell and down cell of the original cell. Similarly, type B ant produces 2 new type A ants in left and right cell and 2 new type B ants in up cell and down cell. The ants produced again reproduce more ants at their adjacent cells according to the same directions given by the their queen ant.

Reproducing ants at adjacent cells take 1 time unit. You need to find the sum of the minimum time in which type A ant reaches the endpoint and the minimum time in which type B ant reaches the endpoint. If any type of ant cannot reach endpoint, the answer is -1.

Constraints

T <= 10
0 < M <= 500
0 < N <= 500
0 <= SX, EX < M
0 <= SY, EY < N

Input

T denotes the number of testcases.
Each testcase consists of 4 lines.
First line gives M , N which is the size of grid
Second line gives starting cell cordinates. SX , SY
Third line gives endpoint cell cordinates  EX , EY
Fourth line gives a permutation of LRUD denoting the directions provided by queen ant to move. The first two characters of the string gives the directions of queen ant type A and last two characters gives direction of queen ant type B.

Output

Print T lines each giving the minimum time in which ant of both types can reach the ending cell for each testcase ie. Find  sum of (minimum time at which A type ant reaches endpoint + minimum time at which B type ant reaches endpoint)

Example

Input:
3
5 5
1 1
3 4
LRUD
2 4
0 2
0 3
RULD
2 4
0 2
1 1
URDL

Output:

10
-1
6

Explanation:

TestCase 1 : Minimum time at which A type ant reaches end point = 5
Minimum time at which B type ant reaches endpoint = 5
TestCase 2 : B cannot reach endpoint. Hence answer is -1.
TestCase 3 : Minimum time at which A type ant reaches end point = 4
Minimum time at which B type ant reaches endpoint = 2