PLUS - On the Plus Side

no tags 

A man called Gerit Teeman was fascinated by the plus symbol and, to be honest, this symbol is actually quite interesting!

Begin by drawing an one. This will be the center of the plus symbol. Above this center draw the number two, below three, to the left four and the the right five. If you then 'replace' the center (either up, down, left or right), do the same, but when a place is already filled you leave it untouched.

To illustrate this we pick the place with the number two: Above it draw a two, the bottom is already filled so we leave it untouched, to the left we draw a four and the the right we draw a five. See the picture below:
example

It is not hard to see that the sum of the first plus symbol is 15 (the first symbol only containing 1 is NOT a plus symbol). The sum of the second symbol is already more difficult, summing up to 26. Now here comes the catch: depending on which number you pick to expand on, the number on positions can change. If we would have picked five to expand on instead of two, we will get the following:
example

As you can see the numbers differ from that of the first example. This time the sum is 25! On top of that, there can be multiple 'replaces', generating more number in the symbol. Your task is, depending on which way you expand to, give the sum of all the elements of plus symbol that you would obtain.

Input

First line contains an integer T (1 <= T <= 100) number of test cases. Each of the next T lines contains a string with letters 'L', 'R', 'U', 'D' and 'S' (separated by commas) which respectively mean left, right, up, down and stop. You should expand to the given direction, based on your last created expansion's center. Stop means you should finish and output the total sum to that point. The number of letters in the string will never exceed 10000.

Note that the first example was created by the string "U,S" and the second by the string "R,S".

Output

For each test case output a single integer in a separate line: the total sum of all numbers contained in the created symbol.

Sample:

Input:
2
U,S
S

Output:
26
15

hide comments
rohit kumar: 2013-08-13 11:41:42

give me tricky test cases

(Tjandra Satria Gunawan)(曾毅昆): 2013-08-13 11:41:42

I don't completely understand the problem.
what is the final "plus" for this case:


input: U,R,S

  2 2
4 2 5 5
4 1 5 5
  3 3

Output: 41

and this case:

Input: R,U,S

  2 2
4 2 2 5
4 1 5 5
  3 3

output: 38

Is that true?

Last edit: 2013-08-13 00:35:33

Added by:Laurens
Date:2013-08-12
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Self created problem