Sunny Island

No attempts yetTime limit1sMemory limit128 MB

Problem

One day a volcano erupted in the middle of the Pacific. After the lava cooled, a new island emerged shaped like a little sun: the land above the water formed a circle (the crater), and the lava that flowed out solidified into straight rays pointing away from the circle.

Tourists soon arrived and market stalls were set up. Stalls stand in three kinds of places.

  • Around the crater rim, stalls are numbered 11 to nn clockwise. Consecutively numbered stalls are neighbours, and stall nn neighbours stall 11, so the rim stalls form a ring.
  • Bridges are built across the crater, each resting on two rim stalls. No two bridges cross or lie above one another. The two stalls joined by a bridge are neighbours.
  • Rays of solidified lava reach outward from certain rim stalls. Along a ray the stalls form a chain: the first ray stall neighbours the rim stall it starts from, and each following stall neighbours the previous one.

A local law forbids two neighbouring stalls from sharing even a single product, so neighbours must sell completely different assortments. Each stall must sell at least a given number of distinct products to stay in business (no stalls may be placed on the bridges).

Determine the smallest number of different products that must be shipped to the island so that every stall can be given at least its required number of products while no two neighbouring stalls share any product.

Input

The first line contains the number of datasets DD (1D201 \le D \le 20). Each dataset is given as follows.

The first line contains nn (3n100003 \le n \le 10000), the number of stalls on the crater rim, numbered 11 to nn clockwise.

The next line contains mm (0mn30 \le m \le n-3), the number of bridges. Each of the following mm lines contains two integers pip_i and kik_i (1pi<kin1 \le p_i < k_i \le n, pi<ki1p_i < k_i - 1, kipin1k_i - p_i \ne n-1): the rim stalls at the two ends of a bridge.

The next line contains rr (0rn0 \le r \le n), the number of lava rays. Each of the following rr lines contains two integers cjc_j and djd_j (1cjn1 \le c_j \le n, 0dj100000 \le d_j \le 10000): the rim stall from which ray jj starts and the number of additional stalls on that ray.

The next line contains nn integers; the ii-th is the number of distinct products the rim stall numbered ii must sell.

Finally there are rr lines; the jj-th contains djd_j integers giving the required product counts of the successive stalls on ray jj, ordered from the stall nearest the crater to the farthest.

There are at most 100000100000 stalls in total, and no stall requires more than 100100 products.

Output

For each dataset, print a single line with the minimum number of different products tt that must be shipped to the island so that every stall receives at least its required number of products and no two neighbouring (or bridge-joined) stalls share any product.

Hint

Figure 1. Map of the island described in the example.