7471: Contest of Rope Pulling

内存限制:300 MB 时间限制:5 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:24 通过:8

题目描述

Rope Pulling, also known as Tug of War, is a classic game. Zhang3 organized a rope pulling contest between Class 1 and Class 2.
There are nstudents in Class 1 and mstudents in Class 2. The ithstudent has strength wiand beauty-value vi. Zhang3 needs to choose some students from both classes, and let those chosen from Class 1 compete against those chosen from Class 2. It is also allowed to choose no students from a class or to choose all of them.
To be a fair contest, the total strength of both teams must be equal. To make the contest more beautiful, Zhang3 wants to choose such a set of students, that the total beauty-value of all participants is maximized. Please help her determine the optimal total beauty-value.
 

输入格式

The first line of the input gives the number of test cases, T(1≤T≤30). Ttest cases follow.
For each test case, the first line contains two integers n,m(1≤n,m≤1000), representing the number of students in Class 1 and Class 2.
Then (n+m)lines follow, describing the students. The ithline contains two integers wi,vi(1≤wi≤1000,−109≤vi≤109), representing the strength and the beauty-value of the ithstudent. The first nstudents come from Class 1, while the other mstudents come from Class 2.
The sum of (n+m)in all test cases doesn't exceed 104.

输出格式

For each test case, print a line with an integer, representing the optimal total beauty-value.

输入样例 复制

2
3 4
4 7
3 8
2 2
1 4
5 8
1 3
4 4
1 2
1000 -10000
200 3000
800 5000

输出样例 复制

30
0