Investments
Time limit1sMemory limit128 MB
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 (), the number of test sets. The sets are then described one after another.
The first line of a set contains two natural numbers and (). is the number of investments and is the number of possible benefits.
The second line of the set contains natural numbers (), where is the cost of carrying out the -th investment (investments are numbered from to ).
The third line contains natural numbers (), where is the revenue from the -th benefit (benefits are numbered from to ).
The fourth line contains a natural number ().
Each of the next lines contains two natural numbers and separated by a space (, ). This means that achieving the -th benefit requires carrying out the -th investment.
No pair appears in the input more than once.
Output
For each test set, print the maximum achievable profit on its own line.