#P7741. [2019年杭电多校]Rikka with Cake

[2019年杭电多校]Rikka with Cake

Rikka with Cake

Problem Description

Rikka's birthday is on June 12th. The story of this problem happens on that day. Today is Rikka's birthday. Yuta prepares a big cake for her: the shape of this cake is a rectangular of nn centimeters times mm centimeters. With the guidance of a grimoire, Rikka is going to cut the cake. For simplicity, Rikka firstly builds a Cartesian coordinate system on the cake: the coordinate of the left bottom corner is (0,0)(0,0) while that of the right top corner is (n,m)(n,m). There are KK instructions on the grimoire: The iith cut is a ray starting from (xi,yi)(x_i,y_i) while the direction is DiD_i. There are four possible directions: L, passes (xi1,yi)(x_i-1,y_i); R, passes (xi+1,yi)(x_i+1,y_i); U, passes (xi,yi+1)(x_i,y_i+1); D, passes (xi,yi1)(x_i,y_i-1). Take advantage of the infinite power of Tyrant's Eye, Rikka finishes all the instructions quickly. Now she wants to count the number of pieces of the cake. However, since a huge number of cuts have been done, the number of pieces can be very large. Therefore, Rikka wants you to finish this task.

Input

The first line of the input contains a single integer T(1T100)T(1 \leq T \leq 100), the number of the test cases. For each test case, the first line contains three positive integers n,m,K(1n,m109,1K105)n,m,K(1 \leq n,m \leq 10^9, 1 \leq K \leq 10^5), which represents the shape of the cake and the number of instructions on the grimoire. Then KK lines follow, the iith line contains two integers xi,yi(1xi<n,1yi<m)x_i,y_i(1 \leq x_i < n, 1 \leq y_i <m) and a char Di{D_i \in \{'L','R','U','D'}\}, which describes the iith cut. The input guarantees that there are no more than 55 test cases with K>1000K > 1000, and no two cuts share the same xx coordinate or yy coordinate, i.e., 1i<jK\forall 1 \leq i < j \leq K, xixjx_i \neq x_j and yiyjy_i \neq y_j.

Output

For each test case, output a single line with a single integer, the number of pieces of the cake.

Hint

The left image and the right image show the results of the first and the second test case in the sample input respectively. Clearly, the answer to the first test case is 33 while the second one is 55.

Sample Input

2

4 4 3

1 1 U

2 2 L

3 3 L

5 5 4

1 2 R

3 1 U

4 3 L

2 4 D

Sample Output

3

5

Source

2019 Multi-University Training Contest 9