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 $n$ distinct chemical elements with indices $a_1, a_2, \ldots, a_n$, and will get an income of $x_i$ Berland rubles if the $i$-th element from this list is in the set of this company.

The TopChemist company discovered $m$ distinct chemical elements with indices $b_1, b_2, \ldots, b_m$, and it will get an income of $y_j$ Berland rubles for including the $j$-th element from this list to its set.

In other words, the first company can present any subset of elements from $\{a_1, a_2, \ldots, a_n\}$ (possibly empty subset), the second company can present any subset of elements from $\{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 $n$ ($1 \leq n \leq 10^5$)  — the number of elements discovered by ChemForces.

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

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

The $j$-th of the next $m$ lines contains two integers $b_j$ and $y_j$, ($1 \leq b_j \leq 10^9$, $1 \leq y_j \leq 10^9$)  — the index of the $j$-th element and the income of its usage on the exhibition. It is guaranteed that all $b_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.


-----Examples-----
Input
3
1 2
7 2
3 10
4
1 4
2 4
3 4
4 4

Output
24

Input
1
1000000000 239
3
14 15
92 65
35 89

Output
408



-----Note-----

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

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

Difficulty: interview

Starter code:

Solutions:

import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline

d={}

n=int(input())
for _ in range(n):
    i,j=map(int,input().split())
    d[i]=j
m=int(input())
for _ in range(m):
    i,j=map(int,input().split())
    if i in d and d[i]>j: continue
    d[i]=j

ans=0
for i,j in d.items():
    ans+=j
print(ans)

n=int(input())
di={}
for i in range(n):
  a,b=(list(map(int,input().strip().split(' '))))
  try:
    p=di[a]
    di[a]=max(di[a],b)
  except KeyError:
    di[a]=b
m=int(input())
for i in range(m):
  a,b=(list(map(int,input().strip().split(' '))))
  try:
    p=di[a]
    di[a]=max(di[a],b)
  except KeyError:
    di[a]=b
ans=0
for i in di:
  ans+=di[i]
print(ans)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun May 27 20:07:20 2018

@st0rmbring3r
"""

cost_dict = {}

a = int(input())
for i in range(a):
    x,y = [int(x) for x in input().split()]
    cost_dict[x] = y

b = int(input())
for i in range(b):
    x,y = [int(x) for x in input().split()]
    if x in cost_dict:
        y2 = cost_dict[x]
        cost_dict[x] = max(y,y2)
    else:
        cost_dict[x] = y

print(sum(cost_dict.values()))

# python3


def readline(): return tuple(map(int, input().split()))


def readlines(count): return (readline() for __ in range(count))


def main():
    n, = readline()
    z = dict(readlines(n))

    m, = readline()
    for (b, y) in readlines(m):
        z[b] = max(z.setdefault(b, y), y)

    print(sum(z.values()))


main()


a = {}
b = {}
for i in range(int(input())):
    ID, val = [int(j) for j in input().split(' ')]
    a[ID] = val
for i in range(int(input())):
    ID, val = [int(j) for j in input().split(' ')]
    b[ID] = val

badA = []
badB = []
for k in a:
    if k in b and b[k] > a[k]:
        badA.append(k)
    elif k in b:
        badB.append(k)

for k in badA:
    del a[k]
for k in badB:
    del b[k]

s = 0
for k in a:
    s += a[k]
for k in b:
    s += b[k]
print(s)

n=int(input())
s = {}
for i in range(n):
    a , b = list(map(int , input().split()))
    s[a] = b
m = int(input())
for j in range(m):
    a,b = list(map(int , input().split()))
    if a in s:
        s[a] = max(s[a] , b)
    else:
        s[a] = b
ans = 0 
for i in s  :

    ans +=s[i]
print(ans)
    


from collections import defaultdict

n = int(input())
cost = defaultdict(int)
for _ in range(n):
    a, x = [int(x) for x in input().split()]
    cost[a] = max(cost[a], x)

m = int(input())

for _ in range(m):
    a, x = [int(x) for x in input().split()]
    cost[a] = max(cost[a], x)

print(sum(cost.values()))


elA = dict()
elAs = set()
elB = dict()
elBs = set()
s = 0
n = int(input())
for i in range(n):
    x, y = list(map(int, input().split()))
    elA[x] = y
    elAs.add(x)
m = int(input())
for i in range(m):
    x, y = list(map(int, input().split()))
    elB[x] = y
    elBs.add(x)
w = elAs & elBs
for k in w: s += max(elA[k], elB[k])
for k in elAs:
    if k not in w: s += elA[k]
for k in elBs:
    if k not in w: s += elB[k]
print(s)


d = {}
n = int(input())
for i in range(n):
    x, y = list(map(int, input().split()))
    if x in d:
        d[x] = max(d[x], y)
    else:
        d[x] = y
n = int(input())
for i in range(n):
    x, y = list(map(int, input().split()))
    if x in d:
        d[x] = max(d[x], y)
    else:
        d[x] = y
s = 0
for a in d:
    s += d[a]
print(s)

def getIntList():
    return list(map(int, input().split()));
def getTransIntList(n):
    first=getIntList();
    m=len(first);
    result=[[0]*n for _ in range(m)];
    for i in range(m):
        result[i][0]=first[i];
    for j in range(1, n):
        curr=getIntList();
        for i in range(m):
            result[i][j]=curr[i];
    return result;
n=int(input());
a, x=getTransIntList(n);
m=int(input());
b, y=getTransIntList(m);
def solve():
    d=dict();
    for i in range(n):
        currA=a[i];
        currX=x[i];
        d[currA]=currX;
    for j in range(m):
        currB=b[j];
        currY=y[j];
        d[currB]=max(d.get(currB, 0), currY);
    result=0;
    for key in list(d.keys()):
        result+=d[key];
    return result;
print((solve()));


n=int(input())
ans={}
for i in range(n):
    a,x=list(map(int,input().split()))
    ans[a]=x
m=int(input())
for i in range(m):
    a,x=list(map(int,input().split()))
    if a in ans:
        if ans[a]<x:
            ans[a]=x
    else:
        ans[a]=x
s=0
for i in ans:
    s+=ans[i]
print(s)


d = {}
n = int(input())
for i in range(n):
    j, k = list(map(int, input().split()))
    d[j] = k

m = int(input())
for i in range(m):
    j, k = list(map(int, input().split()))
    if j in d:
        d[j] = max(d[j], k)
    else:
        d[j] = k

ss = 0
for i, v in d.items():
    # print(i, v)
    ss += v
print(ss)

n=int(input())
sum1=0
fo=dict()
for i in range(n):
    x,y=map(int,input().split())
    fo[x]=y
    sum1+=y
to=dict()
m=int(input())
for i in range(m):
    x,y=map(int,input().split())
    if(x in fo):
        if(fo[x]<y):
            sum1-=fo[x]
            sum1+=y
    else:
        sum1+=y
print(sum1)

n = int(input())
a = {}
for _ in range(n):
    aa, xx = [int(v) for v in input().split()]
    a[aa] = xx

m = int(input())
b = {}
for _ in range(m):
    bb, yy = [int(v) for v in input().split()]
    b[bb] = yy

sa = set(a.keys())
sb = set(b.keys())
both = sa & sb

print(
    sum(max(a[v], b[v]) for v in both) +
    sum(a[v] for v in sa - both) +
    sum(b[v] for v in sb - both)
)

d = {}

n = int(input())
for i in range(n):
    num,pro = map(int,input().split())
    try:
        d[num] = max(d[num],pro)
    except:
        d[num]=pro
m = int(input())
for i in range(m):
    num,pro = map(int,input().split())
    try:
        d[num] = max(d[num],pro)
    except:
        d[num]=pro
ans = 0 
for ele in d:
    ans+=d[ele]
print(ans)

from collections import defaultdict as dd, deque

E = set()

n = int(input())
C1 = dd(int)
for _ in range(n):
    i,c = list(map(int,input().split()))
    C1[i] = c
    E.add(i)

m = int(input())
C2 = dd(int)
for _ in range(m):
    i,c = list(map(int,input().split()))
    C2[i] = c
    E.add(i)

e = 0
for el in E:
    e += max(C1[el],C2[el])
print(e)


import sys
#sys.stdin = open("2.in") # COMMENT THIS BEFORE SUBMIT
input = sys.stdin.readline

N = int(input())
A = set()
X = {}
for i in range(N):
	a,x = map(int,input().split())
	A.add(a)
	X[a] = x
M = int(input())
B = set()
Y = {}
for i in range(M):
	b,y = map(int,input().split())
	B.add(b)
	Y[b] = y

s1 = sum([X[x] for x in A - B])
s2 = sum([Y[y] for y in B - A])
s3 = sum([max(X[x],Y[x]) for x in A & B])
print(s1+s2+s3)

m=int(input())
d={}
for i in range(m):
  a,v=map(int,input().split())
  d[a]=v
n=int(input())
for i in range(n):
  a,v=map(int,input().split())
  if(a in d):
    d[a]=max(d[a],v)
  else:
    d[a]=v
#print(d)
l=d.values()
print(sum(l))

#!/usr/bin/env python3

n = int(input().strip())
a = [tuple(map(int, input().strip().split())) for _ in range(n)]
m = int(input().strip())
a.extend(tuple(map(int, input().strip().split())) for _ in range(m))

a.sort()
a.append((0, 0))
res = 0
i = 0
while i < n + m:
	if a[i][0] == a[i + 1][0]:
		res += max(a[i][1], a[i + 1][1])
		i += 2
	else:
		res += a[i][1]
		i += 1


print (res)


n=int(input())
d=dict()
for i in range(n):
    a,b=list(map(int,input().split()))
    try:
        d[a]=max(d[a],b)
    except:
        d.update({a:b})
m=int(input())
for i in range(m):
    a,b=list(map(int,input().split()))
    try:
        d[a]=max(d[a],b)
    except:
        d.update({a:b})
ans=0
for i in d:
    ans+=d[i]
print(ans)


from collections import defaultdict as dd
d = dd(int)

n = int(input())
for _ in range(n):
    a, x = list(map(int, input().split()))
    d[a] = x
m = int(input())
for _ in range(m):
    b, y = list(map(int, input().split()))
    if y > d[b]:
        d[b] = y
print(sum(d.values()))


def solution():
    n = int(input())
    d1, d2 = dict(), dict()
    for _ in range(n):
        key, value = list(map(int, input().split()))
        d1[key] = value
    m = int(input())
    for _ in range(m):
        key, value = list(map(int, input().split()))
        d2[key] = value

    for key, value in list(d2.items()):
        if key in d1:
            if d2[key] > d1[key]:
                d1[key] = value
        else:
            d1[key] = value
    print(sum(d1.values()))



solution()


se = set()
n = int(input())
cf = {}
for i in range(n):
    a,x = [int(s) for s in input().split()]
    cf[a] = x
    se.add(a)
m = int(input())
tc = {}
for i in range(m):
    b,y = [int(s) for s in input().split()]
    tc[b] = y
    se.add(b)

sum = 0
for e in se:
    sum += max(cf.get(e, 0), tc.get(e, 0))

print(sum)

a_size = int(input())
elements = {}
for _ in range(a_size):
    index, price = map(int, input().split())
    elements[index] = price
    
b_size = int(input())
for _ in range(b_size):
    index, price = map(int, input().split())
    elements[index] = max(price, elements.get(index, 0))
    
print(sum(elements.values()))

n = int(input())
chemicals = {}
for i in range(n):
    line = [int(el) for el in input().split()]
    chemicals.update({line[0]: [line[1], 0]})
m = int(input())
for i in range(m):
    line = [int(el) for el in input().split()]
    if line[0] not in chemicals.keys():
        chemicals.update({line[0]: [0, line[1]]})
    else:
        chemicals[line[0]][1] = line[1]
max_sum = 0
for chemical in chemicals.keys():
    max_sum += max(chemicals[chemical])
print(max_sum)