Slicing Tree

No attempts yetTime limit1sMemory limit128 MB

Problem

VLSI circuits are so complex, reaching millions of transistors in a single chip today, that they cannot be designed without CAD tools. To reduce the complexity of the design process, the whole flow is broken into several intermediate phases, one of which is the physical design phase. In physical design the basic components of a circuit are usually treated as rectangular modules. Within this phase there are several steps, and the one most critical to circuit performance is the floorplan/placement step. The real floorplan/placement problem is very complex, so here you only handle a simplified version in which every component is viewed as a single rectangle in the plane.

The simplified problem is to place nn rectangles in the plane, all axis-parallel, so that a set of given constraints is satisfied. The constraints describe relative locations between rectangles. A relative location means that rectangle RiR_i (1in1 \le i \le n) must be placed either below (or above) rectangle RjR_j (1jn1 \le j \le n), or to the left (or right) of it. For each rectangle RiR_i, the coordinates of its lower-left corner and its upper-right corner are written as (xill,yill)(x_i^{ll}, y_i^{ll}) and (xiur,yiur)(x_i^{ur}, y_i^{ur}). Saying that RiR_i is below RjR_j means yiuryjlly_i^{ur} \le y_j^{ll}, and saying that RiR_i is to the left of RjR_j means xiurxjllx_i^{ur} \le x_j^{ll}. Each rectangle may also be placed rotated by 9090^\circ. Note that a single constraint such as "R1R_1 is below R2R_2" can still be satisfied by more than one placement.

The relative-location constraints are represented by a binary tree called a slicing tree. A slicing tree describes how the plane is partitioned and which rectangle goes into each sub-region. Every internal node is labeled either HH or VV, and every external node (leaf) is labeled with a rectangle identification number ii (1in1 \le i \le n). Label HH means the sub-region is partitioned by a horizontal line, and label VV means it is partitioned by a vertical line. For instance, in a tree with only two leaves, a root labeled HH means R1R_1 must be placed below R2R_2, while a root labeled VV means R1R_1 must be placed to the left of R2R_2.

In general, if an internal node NkN_k is labeled HH, then every rectangle in the left subtree of NkN_k must be placed below every rectangle in the right subtree. Likewise, if NkN_k is labeled VV, every rectangle in the left subtree must be placed to the left of every rectangle in the right subtree.

Once a placement is fixed, let R\boxed{R} denote the minimum rectangle that encloses all the rectangles. Given a slicing tree and the dimensions of the rectangles, your program must determine a location (and orientation) for each rectangle so that all constraints are satisfied and the area of the enclosing rectangle R\boxed{R} is as small as possible. Note that the area of R\boxed{R} can depend on the orientation chosen for each rectangle.

Input

Your program reads from standard input. The input consists of TT test cases, and the number of test cases TT is given on the first line.

Each test case begins with a line containing an integer nn (1n10001 \le n \le 1000), the number of rectangles. The next nn lines give the dimensions of the rectangles, one per line. The ii-th line (1in1 \le i \le n) contains two integers ww and hh (1w,h5001 \le w, h \le 500), the width and height of rectangle ii.

The following line describes the slicing tree. It is a list of 2n12n - 1 items separated by spaces, obtained by traversing the slicing tree in post-order. Each item is either an internal-node label or an external-node label. An internal-node label is HH or VV, and an external-node label is an integer ii (1in1 \le i \le n), the rectangle identification number.

Output

Your program writes to standard output. Print exactly one line for each test case. For each test case, find a placement that satisfies all relative-location constraints and minimizes the area of the enclosing rectangle R\boxed{R}, then print that minimum area on its own line. You may assume that the resulting area of R\boxed{R} is less than 101010^{10} for every test case.