How Big Are the Pockets? (Small)

Walk a closed non-self-touching orthogonal path from run-length encoded moves and sum the area of pockets: cells outside the polygon with boundary both east and west or north and south.

Medium7GeometrySimulationImplementationNo attempts yetTime limit5sMemory limit512 MB

Problem

Professor Polygonovich, an honest citizen of Flatland, likes to walk along the integer points of the plane. He starts at the origin in the morning, facing north, and he makes only three kinds of moves.

  • 'F': move one unit forward.
  • 'L': turn 90 degrees to the left.
  • 'R': turn 90 degrees to the right.

At the end of the day (yes, it is a long walk) he is back at the origin. Apart from the origin he never visits the same point twice, so his path encloses a polygon. In the picture below the interior of the polygon is blue. The points xx, yy, zz and ww are explained further down.

Once the professor makes more than 4 turns the polygon is not convex, so it has pockets.

Careful! The definition of a pocket used here may differ from the one you already know.

The gray area below is the set of pockets of the polygon.

Formally, a point pp is in a pocket if pp is not inside the polygon and at least one of the following two conditions holds.

  • There are boundary points directly east of pp and directly west of pp.
  • There are boundary points directly north of pp and directly south of pp.

The boundary points are the points the professor walks over. They include every point of the traversed segments, not only the points with integer coordinates.

Look at the first picture again. Point xx satisfies the first condition, point zz satisfies the second one, and point yy satisfies both. All three points are in pockets. Point ww is not in a pocket.

Given the professor's walk, compute the total area of the pockets.

Input

The first line contains the number of test cases NN. The NN test cases follow.

Each test case describes one walk. It starts with an integer LL, followed by LL pairs of the form "SS TT", where SS is a string of the characters 'L', 'R' and 'F', and TT is an integer telling how many times SS is repeated.

So the input of one test case looks like this.

S1 T1 S2 T2 ... SL TL

The moves the professor makes are T1T_1 copies of S1S_1, then T2T_2 copies of S2S_2, and so on.

The pairs of one test case are not necessarily all on the same line, but a single string SS is never split across lines.

Limits

  • 1N1001 \le N \le 100
  • 1L1001 \le L \le 100
  • the length of each string SS is between 1 and 16, inclusive
  • 1T1 \le T
  • the concatenated path never changes direction twice in a row, so none of 'LL', 'RR', 'LR' and 'RL' occurs in it, and it contains at least one 'F'
  • the path ends at the origin and does not touch itself anywhere else
  • the professor never visits a point with a coordinate larger than 100 in absolute value, and that condition also bounds the length of the concatenated move string

Output

For each test case print one line in the format Case #X: Y, where XX is the 1-based number of the test case and YY is the total area of the pockets. That area is always an integer, so print it as an integer with no decimal point.

Note

The picture below draws the two walks of the example test case.