CF981B.Businessmen Problems

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Businessmen Problems

Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.

In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies.

All elements are enumerated with integers. The ChemForces company has discovered nn distinct chemical elements with indices a1,a2,,ana_1, a_2, \ldots, a_n, and will get an income of xix_i Berland rubles if the ii-th element from this list is in the set of this company.

The TopChemist company discovered mm distinct chemical elements with indices b1,b2,,bmb_1, b_2, \ldots, b_m, and it will get an income of yjy_j Berland rubles for including the jj-th element from this list to its set.

In other words, the first company can present any subset of elements from {a1,a2,,an}\{a_1, a_2, \ldots, a_n\} (possibly empty subset), the second company can present any subset of elements from {b1,b2,,bm}\{b_1, b_2, \ldots, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets.

Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible.

Input

The first line contains a single integer nn (1n1051 \leq n \leq 10^5)  — the number of elements discovered by ChemForces.

The ii-th of the next nn lines contains two integers aia_i and xix_i (1ai1091 \leq a_i \leq 10^9, 1xi1091 \leq x_i \leq 10^9)  — the index of the ii-th element and the income of its usage on the exhibition. It is guaranteed that all aia_i are distinct.

The next line contains a single integer mm (1m1051 \leq m \leq 10^5)  — the number of chemicals invented by TopChemist.

The jj-th of the next mm lines contains two integers bjb_j and yjy_j, (1bj1091 \leq b_j \leq 10^9, 1yj1091 \leq y_j \leq 10^9)  — the index of the jj-th element and the income of its usage on the exhibition. It is guaranteed that all bjb_j are distinct.

Output

Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.

Note

In the first example ChemForces can choose the set (3,73, 7), while TopChemist can choose (1,2,41, 2, 4). This way the total income is (10+2)+(4+4+4)=24(10 + 2) + (4 + 4 + 4) = 24.

In the second example ChemForces can choose the only element 10910^9, while TopChemist can choose (14,92,3514, 92, 35). This way the total income is (239)+(15+65+89)=408(239) + (15 + 65 + 89) = 408.

Samples

3
1 2
7 2
3 10
4
1 4
2 4
3 4
4 4
24
1
1000000000 239
3
14 15
92 65
35 89
408

在线编程 IDE

建议全屏模式获得最佳体验