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 n 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 Ri (1≤i≤n) must be placed either below (or above) rectangle Rj (1≤j≤n), or to the left (or right) of it. For each rectangle Ri, the coordinates of its lower-left corner and its upper-right corner are written as (xill,yill) and (xiur,yiur). Saying that Ri is below Rj means yiur≤yjll, and saying that Ri is to the left of Rj means xiur≤xjll. Each rectangle may also be placed rotated by 90∘. Note that a single constraint such as "R1 is below R2" 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 H or V, and every external node (leaf) is labeled with a rectangle identification number i (1≤i≤n). Label H means the sub-region is partitioned by a horizontal line, and label V means it is partitioned by a vertical line. For instance, in a tree with only two leaves, a root labeled H means R1 must be placed below R2, while a root labeled V means R1 must be placed to the left of R2.
In general, if an internal node Nk is labeled H, then every rectangle in the left subtree of Nk must be placed below every rectangle in the right subtree. Likewise, if Nk is labeled V, 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 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 is as small as possible. Note that the area of R can depend on the orientation chosen for each rectangle.
Your program reads from standard input. The input consists of T test cases, and the number of test cases T is given on the first line.
Each test case begins with a line containing an integer n (1≤n≤1000), the number of rectangles. The next n lines give the dimensions of the rectangles, one per line. The i-th line (1≤i≤n) contains two integers w and h (1≤w,h≤500), the width and height of rectangle i.
The following line describes the slicing tree. It is a list of 2n−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 H or V, and an external-node label is an integer i (1≤i≤n), the rectangle identification number.
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, then print that minimum area on its own line. You may assume that the resulting area of R is less than 1010 for every test case.