406805: GYM102562 F Friendly Game

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

F. Friendly Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Ghita and Lica decided to play $$$T$$$ friendly games. Since he is competitive, your task is to help Ghita win every single one of them.

In this game, you are given a matrix of $$$N$$$ rows and $$$M$$$ columns. You earn a point by placing a marble in any cell of said matrix. However, there is a rule that Lica has imposed on Ghita and which you must follow - on each row and column, you are only allowed to place a certain amount of marbles at most.

In order for Ghita to win, you must place the marbles in such a way that all the requirements are fulfilled while maximising the total number of points.

Input

The first line has an integer T ($$$1\leq T \leq 10^5$$$) - the number of games.

On the first line of a test's input, there are two integers, namely: $$$N$$$, the number of rows and $$$M$$$, the number of columns.

The second line of input contains N values $$$a_i$$$ ($$$1 \leq i \leq N$$$, $$$0 \leq a_i \leq M)$$$ - the maximum number of marbles that can be placed on row $$$i$$$.

The third line of input contains M positive integers $$$b_j$$$ ($$$1 \leq j \leq M$$$, $$$0 \leq b_j \leq N$$$) - the maximum number of marbles that can be placed on column $$$j$$$.

Please note that the sum of $$$N$$$, respectively the sum of $$$M$$$ will not exceed $$$10^6$$$ over all test cases.

Output

For each test, you ought to print, on a line, a value $$$x$$$ which represents the maximum number of points that can be obtained while satisfying Lica's conditions.

ExampleInput
1
4 4
1 1 3 4
2 2 2 4
Output
9

加入题单

算法标签: