This page is still under construction.

Parts of this page are still being built. What you see may change.

Investments

Time limit1sMemory limit128 MB

Summary
Pick some priced investments to unlock revenue benefits that each require a set of investments and maximize revenue minus cost.
Level

Medium7 of 10

Topics
Graph
Solved
No attempts yet

Problem

Investing is serious business! Mr. Jozef, a businessman in the farming and livestock sector (also known from the Zalew problem), knows this very well, and he has just started planning his budget for the upcoming year 2011.

Mr. Jozef has prepared a list of investments he could carry out on his farm (for example: building a wind power plant, a new barn, creating a website, or introducing ecological fertilizers), together with the cost of each one.

Of course, the goal of investing is to earn high profits. Mr. Jozef knows that certain combinations of investments will bring him very specific benefits. For example:

  • Introducing ecological fertilizers and building a wind power plant (both must be done together!) will earn Mr. Jozef the award for the most ecological farmer of the year.
  • Building a wind power plant will save a specific amount on the electricity bills.

(as the example above shows, a single investment can contribute to several different benefits at the same time)

Given the cost of each investment, the revenue that each achieved benefit brings, and which investments are required to achieve each benefit, compute the maximum profit (the sum of revenues from the achieved benefits minus the sum of costs of the investments carried out) that Mr. Jozef can achieve in 2011.

The list of achievable benefits may include some that require no investments at all.

Input

The first line contains a single natural number ZZ (1≤Z≤101 \le Z \le 10), the number of test sets. The sets are then described one after another.

The first line of a set contains two natural numbers AA and BB (1≤A,B≤1001 \le A, B \le 100). AA is the number of investments and BB is the number of possible benefits.

The second line of the set contains AA natural numbers aia_i (1≤ai≤10000001 \le a_i \le 1000000), where aia_i is the cost of carrying out the ii-th investment (investments are numbered from 11 to AA).

The third line contains BB natural numbers bib_i (1≤bi≤10000001 \le b_i \le 1000000), where bib_i is the revenue from the ii-th benefit (benefits are numbered from 11 to BB).

The fourth line contains a natural number KK (1≤K≤A⋅B1 \le K \le A \cdot B).

Each of the next KK lines contains two natural numbers xix_i and yiy_i separated by a space (1≤xi≤A1 \le x_i \le A, 1≤yi≤B1 \le y_i \le B). This means that achieving the yiy_i-th benefit requires carrying out the xix_i-th investment.

No pair (xi,yi)(x_i, y_i) appears in the input more than once.

Output

For each test set, print the maximum achievable profit on its own line.

Examples1

  1. Example 1

    Input
    1
    3 4
    10 20 10
    5 30 5 5
    4
    1 1
    1 2
    2 2
    3 3
    
    Expected output
    10