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$.

Source: codeforces | Difficulty: EASY
Tags: Sorting


Solutions:
def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def MAP2():
	return map(float, input().split())

def LIST():
	return list(map(int, input().split()))

def STRING():
	return input()
import string
import sys
from heapq import heappop, heappush
from bisect import *
from collections import deque, Counter, defaultdict
from math import *
from itertools import permutations, accumulate
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
n = INT()
l = []
d = {}
sm = 0
for i in range(n):
	(indx, y) = MAP()
	d[indx] = y
	sm += y
m = INT()
for i in range(m):
	(indx, y) = MAP()
	if indx in d:
		sm -= d[indx]
		sm += max(y, d[indx])
	else:
		sm += y
print(sm)


import sys, collections
res = dict()
n = int(sys.stdin.readline())
for _ in range(n):
	(a, b) = map(int, sys.stdin.readline().split())
	res[a] = b
m = int(sys.stdin.readline())
for _ in range(m):
	(a, b) = map(int, sys.stdin.readline().split())
	if a in res:
		res[a] = max(res[a], b)
	else:
		res[a] = b
ans = 0
for i in res:
	ans += res[i]
print(ans)


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)


n = int(input())
d = {}
s = 0
for i in range(n):
	(a, b) = map(int, input().split())
	try:
		if d[a] < b:
			s -= d[a]
			d[a] = b
			s += b
	except:
		d[a] = b
		s += b
m = int(input())
for i in range(m):
	(a, b) = map(int, input().split())
	try:
		if d[a] < b:
			s -= d[a]
			d[a] = b
			s += b
	except:
		d[a] = b
		s += b
print(s)


n = int(input())
from collections import defaultdict
a = defaultdict(list)
for i in range(n):
	(x, y) = map(int, input().split())
	a[x].append(y)
m = int(input())
for i in range(m):
	(x, y) = map(int, input().split())
	a[x].append(y)
ans = 0
for i in a:
	ans += max(a[i])
print(ans)


from sys import stdin

def main():
	l = stdin.read().splitlines()
	(d, s, n) = ({}, 0, int(l[0]))
	for i in range(1, n + 1):
		(a, x) = map(int, l[i].split())
		d[a] = x
		s += x
	for i in range(n + 2, n + 2 + int(l[n + 1])):
		(a, y) = map(int, l[i].split())
		x = d.get(a, 0)
		if x < y:
			s += y - x
	print(s)
main()


n = int(input())
(a, b) = (dict(), dict())
for _ in range(n):
	(key, value) = map(int, input().split())
	a[key] = value
m = int(input())
for _ in range(m):
	(key, value) = map(int, input().split())
	b[key] = value
for (key, value) in b.items():
	if key in a:
		if b[key] > a[key]:
			a[key] = value
	else:
		a[key] = value
print(sum(a.values()))


from sys import stdin, stdout
import heapq
import cProfile, math
from collections import Counter, defaultdict, deque
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
import sys
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)
fac_warm_up = False
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7

class MergeFind:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n
		self.lista = [[_] for _ in range(n)]

	def find(self, a):
		to_update = []
		while a != self.parent[a]:
			to_update.append(a)
			a = self.parent[a]
		for b in to_update:
			self.parent[b] = a
		return self.parent[a]

	def merge(self, a, b):
		a = self.find(a)
		b = self.find(b)
		if a == b:
			return
		if self.size[a] < self.size[b]:
			(a, b) = (b, a)
		self.num_sets -= 1
		self.parent[b] = a
		self.size[a] += self.size[b]
		self.lista[a] += self.lista[b]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets

def display(string_to_print):
	stdout.write(str(string_to_print) + '\n')

def prime_factors(n):
	factors = dict()
	for i in range(2, math.ceil(math.sqrt(n)) + 1):
		while n % i == 0:
			if i in factors:
				factors[i] += 1
			else:
				factors[i] = 1
			n = n // i
	if n > 2:
		factors[n] = 1
	return factors

def all_factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def fibonacci_modP(n, MOD):
	if n < 2:
		return 1
	return (cached_fn(fibonacci_modP, (n + 1) // 2, MOD) * cached_fn(fibonacci_modP, n // 2, MOD) + cached_fn(fibonacci_modP, (n - 1) // 2, MOD) * cached_fn(fibonacci_modP, (n - 2) // 2, MOD)) % MOD

def factorial_modP_Wilson(n, p):
	if p <= n:
		return 0
	res = p - 1
	for i in range(n + 1, p):
		res = res * cached_fn(InverseEuler, i, p) % p
	return res

def binary(n, digits=20):
	b = bin(n)[2:]
	b = '0' * (digits - len(b)) + b
	return b

def is_prime(n):
	if n < 4:
		return True
	if n % 2 == 0:
		return False
	if n % 3 == 0:
		return False
	i = 5
	w = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += w
		w = 6 - w
	return True

def generate_primes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	return prime
factorial_modP = []

def warm_up_fac(MOD):
	global factorial_modP, fac_warm_up
	if fac_warm_up:
		return
	factorial_modP = [1 for _ in range(fac_warm_up_size + 1)]
	for i in range(2, fac_warm_up_size):
		factorial_modP[i] = factorial_modP[i - 1] * i % MOD
	fac_warm_up = True

def InverseEuler(n, MOD):
	return pow(n, MOD - 2, MOD)

def nCr(n, r, MOD):
	global fac_warm_up, factorial_modP
	if not fac_warm_up:
		warm_up_fac(MOD)
		fac_warm_up = True
	return factorial_modP[n] * (pow(factorial_modP[r], MOD - 2, MOD) * pow(factorial_modP[n - r], MOD - 2, MOD) % MOD) % MOD

def test_print(*args):
	if testingMode:
		print(args)

def display_list(list1, sep=' '):
	stdout.write(sep.join(map(str, list1)) + '\n')

def display_2D_list(li):
	for i in li:
		print(i)

def prefix_sum(li):
	sm = 0
	res = []
	for i in li:
		sm += i
		res.append(sm)
	return res

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))
memory = dict()

def clear_cache():
	global memory
	memory = dict()

def cached_fn(fn, *args):
	global memory
	if args in memory:
		return memory[args]
	else:
		result = fn(*args)
		memory[args] = result
		return result

def ncr(n, r):
	return math.factorial(n) / (math.factorial(n - r) * math.factorial(r))

def binary_search(i, li):
	fn = lambda x: li[x] - x // i
	x = -1
	b = len(li)
	while b >= 1:
		while b + x < len(li) and fn(b + x) > 0:
			x += b
		b = b // 2
	return x
TestCases = False
fac_warm_up_size = 10 ** 5 + 100
optimise_for_recursion = False

def main():
	n = get_int()
	la = []
	for i in range(n):
		tp = get_list()
		la.append(tp)
	m = get_int()
	for j in range(m):
		tp = get_list()
		la.append(tp)
	sm = 0
	i = 0
	la.sort()
	while i < len(la):
		if i + 1 != len(la) and la[i][0] == la[i + 1][0]:
			sm += max(la[i][1], la[i + 1][1])
			i += 2
		else:
			sm += la[i][1]
			i += 1
	print(sm)
if TestCases:
	for i in range(get_int()):
		main()
else:
	main() if not optimise_for_recursion else threading.Thread(target=main).start()


_ = int(input())
company = {}
for i in range(_):
	(x, y) = map(int, input().split())
	company[str(x)] = y
for i in range(int(input())):
	(x, y) = map(int, input().split())
	if str(x) in company.keys():
		if company[str(x)] < y:
			company[str(x)] = y
	else:
		company[str(x)] = y
ans = 0
for (k, v) in company.items():
	ans += v
print(ans)


import sys
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)


x = dict()
for i in range(int(input())):
	(k, v) = map(int, input().split())
	if k not in x.keys():
		x[k] = v
	elif x[k] < v:
		x[k] = v
for i in range(int(input())):
	(k, v) = map(int, input().split())
	if k not in x.keys():
		x[k] = v
	elif x[k] < v:
		x[k] = v
print(sum((v for v in x.values())))


n = int(input())
(a, x, s) = (set(), {}, 0)
for i in range(n):
	(ai, xi) = map(int, input().split())
	a.add(ai)
	x[ai] = xi
	s += xi
m = int(input())
(b, y) = (set(), {})
for i in range(m):
	(bi, yi) = map(int, input().split())
	b.add(bi)
	y[bi] = yi
	s += yi
sp = a & b
for i in sp:
	s -= min(x[i], y[i])
print(s)


from collections import defaultdict
dic = defaultdict(int)

def fun(n):
	for i in range(n):
		(x, y) = map(int, input().split())
		dic[x] = max(dic[x], y)
n = int(input())
fun(n)
n = int(input())
fun(n)
print(sum((y for (x, y) in dic.items())))


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)


cforce_count = int(input())
c_dic = {}
for _ in range(cforce_count):
	arr = input().split()
	c_dic[int(arr[0])] = int(arr[1])
tchem_count = int(input())
t_dic = {}
for _ in range(tchem_count):
	arr = input().split()
	t_dic[int(arr[0])] = int(arr[1])
total = 0
for (key, val) in c_dic.items():
	if key in t_dic and t_dic[key] > val:
		continue
	elif key in t_dic and t_dic[key] <= val:
		del t_dic[key]
		total += val
	else:
		total += val
for (key, val) in t_dic.items():
	total += val
print('%d' % total)


dic = dict()
for i in range(int(input())):
	tst = input().split()
	dic[tst[0]] = max(dic.get(tst[0], 0), int(tst[1]))
for i in range(int(input())):
	tst = input().split()
	dic[tst[0]] = max(dic.get(tst[0], 0), int(tst[1]))
print(sum(list(dic.values())))


n = int(input())
s = {}
for i in range(n):
	(a, b) = map(int, input().split())
	s[a] = b
m = int(input())
for j in range(m):
	(a, b) = 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)


n = int(input())
s = {}
for i in range(n):
	(a, b) = map(int, input().split())
	s[a] = b
m = int(input())
for i in range(m):
	(a, b) = map(int, input().split())
	s[a] = max(s.get(a, 0), b)
print(sum(s.values()))


n = int(input())
adict = {}
for _ in range(n):
	(p, q) = map(int, input().split())
	adict.update({p: q})
m = int(input())
for qw in range(m):
	(r, s) = map(int, input().split())
	if r in adict:
		adict.update({r: max(adict[r], s)})
	else:
		adict.update({r: s})
print(sum(adict.values()))


d = {}
n = int(input())
for _ in range(n):
	(a, x) = map(int, input().split())
	d[a] = x
n = int(input())
for _ in range(n):
	(a, x) = map(int, input().split())
	d[a] = max(x, d.get(a, 0))
print(sum(d.values()))


a = {}
b = {}
n = int(input())
for i in range(n):
	(temp1, temp2) = map(int, input().strip().split())
	a[temp1] = temp2
m = int(input())
for i in range(m):
	(temp1, temp2) = map(int, input().strip().split())
	b[temp1] = temp2
A = set(a.keys())
B = set(b.keys())
inboth = A.intersection(B)
A -= inboth
B -= inboth
summ = 0
for i in A:
	summ += a[i]
for i in B:
	summ += b[i]
for i in inboth:
	if a[i] >= b[i]:
		summ += a[i]
	else:
		summ += b[i]
print(summ)


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)


from array import array
database = {}
result = 0
n = int(input())
i = 0
while i < n:
	i += 1
	[a, x] = [int(x) for x in input().split()]
	result += x
	database[a] = x
m = int(input())
i = 0
while i < m:
	i += 1
	[b, y] = [int(x) for x in input().split()]
	x = database.get(b, 0)
	if x < y:
		result += y - x
print(result)


cnt = lambda s, i: s.count(i)
ii = lambda : int(input())
si = lambda : input()
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
n = ii()
p = 0
d = {}
for i in range(n):
	(a, b) = f()
	d[a] = b
m = ii()
for i in range(m):
	(a, b) = f()
	d[a] = max(d.get(a, 0), b)
print(sum(d.values()))


d = dict()
n = int(input())
for i in range(n):
	(x, y) = map(int, input().split())
	d[x] = y
m = int(input())
for i in range(m):
	(x, y) = map(int, input().split())
	if x not in d.keys():
		d[x] = y
	else:
		d[x] = max(d[x], y)
ans = 0
for x in d.keys():
	ans += d[x]
print(ans)


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


from sys import stdin
all_in = stdin.read().splitlines()
n = int(all_in[0])
(a, x) = (list(), list())
for el in all_in[1:n + 1]:
	(a_, x_) = map(int, el.split())
	a.append(a_)
	x.append(x_)
m = int(all_in[n + 1])
(b, y) = (list(), list())
for el in all_in[n + 2:m + n + 2]:
	(b_, y_) = map(int, el.split())
	b.append(b_)
	y.append(y_)
s_a = set(a)
s_b = set(b)
and_ = s_a & s_b
first = {a[i]: x[i] for i in range(n)}
second = {b[j]: y[j] for j in range(m)}
ans = 0
for el in s_a | s_b:
	if el in and_:
		ans += max(first[el], second[el])
	elif el in s_a:
		ans += first[el]
	elif el in s_b:
		ans += second[el]
	else:
		exit(100500)
print(ans)


n = int(input())
prices1 = {}
for i in range(n):
	temp = [int(s) for s in input().split(' ')]
	prices1[temp[0]] = [temp[1]]
m = int(input())
for i in range(m):
	temp = [int(s) for s in input().split(' ')]
	prices1.setdefault(temp[0], []).append(temp[1])
sum = 0
for (key, value) in prices1.items():
	sum += max(value)
print(sum)


allchem = set()
prices = dict()
n = int(input())
for i in range(n):
	(a, b) = input().split(' ')
	a = int(a)
	b = int(b)
	allchem.add(a)
	prices[a] = b
n = int(input())
for i in range(n):
	(a, b) = input().split(' ')
	a = int(a)
	b = int(b)
	allchem.add(a)
	if a in prices:
		prices[a] = max(prices[a], b)
	else:
		prices[a] = b
suma = 0
allchem = list(allchem)
for i in range(len(allchem)):
	suma += prices[allchem[i]]
print(suma)


n = int(input())
s = {}
for i in range(n):
	(ai, xi) = input().strip().split()
	s[ai] = int(xi)
n2 = int(input())
for i in range(n2):
	(ai, xi) = input().strip().split()
	if ai in s:
		if s[ai] < int(xi):
			s[ai] = int(xi)
	else:
		s[ai] = int(xi)
print(sum(list(s.values())))


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)


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


def solve(num_a, num_b, a_map, b_map):
	a_indices = list(a_map.keys())
	a_indices.sort()
	b_indices = list(b_map.keys())
	b_indices.sort()
	a_idx = 0
	b_idx = 0
	aval_subset = []
	bval_subset = []
	while a_idx < len(a_indices) and b_idx < len(b_indices):
		if a_indices[a_idx] == b_indices[b_idx]:
			if a_map[a_indices[a_idx]] > b_map[b_indices[b_idx]]:
				aval_subset.append(a_map[a_indices[a_idx]])
			else:
				bval_subset.append(b_map[b_indices[b_idx]])
			a_idx += 1
			b_idx += 1
		elif a_indices[a_idx] < b_indices[b_idx]:
			aval_subset.append(a_map[a_indices[a_idx]])
			a_idx += 1
		else:
			bval_subset.append(b_map[b_indices[b_idx]])
			b_idx += 1
	while a_idx < len(a_indices):
		aval_subset.append(a_map[a_indices[a_idx]])
		a_idx += 1
	while b_idx < len(b_indices):
		bval_subset.append(b_map[b_indices[b_idx]])
		b_idx += 1
	return sum(aval_subset) + sum(bval_subset)
num_discovered_a = int(input())
a_mapping = {}
for x1 in range(num_discovered_a):
	(idx, val) = map(int, input().split())
	a_mapping[idx] = val
num_discovered_b = int(input())
b_mapping = {}
for x2 in range(num_discovered_b):
	(idx, val) = map(int, input().split())
	b_mapping[idx] = val
print(solve(num_discovered_a, num_discovered_b, a_mapping, b_mapping))


import sys
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)


def main():
	n = int(input())
	a = set()
	x = dict()
	for _ in range(n):
		(ele, inc) = map(int, input().split())
		a.add(ele)
		x[ele] = inc
	m = int(input())
	b = set()
	y = dict()
	for _ in range(m):
		(ele, inc) = map(int, input().split())
		b.add(ele)
		y[ele] = inc
	both = a & b
	only_a = a - both
	only_b = b - both
	income = 0
	income += sum((x[ele] for ele in only_a))
	income += sum((y[ele] for ele in only_b))
	income += sum((max(x[ele], y[ele]) for ele in both))
	print(income)
main()


I = lambda : [*map(int, input().split())]
d = {}
exec(2 * 'for _ in[0]*I()[0]:\n a,b=I()\n if a in d:d[a]=max(d[a],b)\n else:d[a]=b\n')
print(sum(d.values()))


import sys
n = int(sys.stdin.readline())
d = []
for i in range(n):
	a = list(map(int, sys.stdin.readline().split()))
	d.append(a)
o = int(sys.stdin.readline())
for i in range(o):
	a = list(map(int, sys.stdin.readline().split()))
	d.append(a)
d.sort()
sum = 0
for i in range(n + o):
	if i == 0:
		sum += d[i][1]
	elif d[i][0] == d[i - 1][0]:
		sum += d[i][1] - d[i - 1][1]
	else:
		sum += d[i][1]
print(sum)


n = int(input())
ans = 0
codechef = {}
topcoder = {}
for i in range(n):
	(a, x) = map(int, input().split())
	codechef[a] = x
m = int(input())
for i in range(m):
	(b, x) = map(int, input().split())
	topcoder[b] = x
for item in codechef:
	if item in topcoder:
		ans += max(codechef[item], topcoder[item])
	else:
		ans += codechef[item]
for item in topcoder:
	if item not in codechef:
		ans += topcoder[item]
print(ans)


n = int(input())
ans = {}
for i in range(n):
	(a, x) = map(int, input().split())
	if a not in ans:
		ans[a] = set()
	ans[a].add(x)
m = int(input())
for i in range(m):
	(a, x) = map(int, input().split())
	if a not in ans:
		ans[a] = set()
	ans[a].add(x)
cnt = 0
for j in ans:
	cnt += max(ans[j])
print(cnt)


n = int(input())
d = {}
for i in range(n):
	(a, x) = map(int, input().split())
	d[a] = x
m = int(input())
for i in range(m):
	(b, y) = map(int, input().split())
	if b in d:
		if d[b] < y:
			d[b] = y
	else:
		d[b] = y
ans = 0
for (key, value) in d.items():
	ans += value
print(ans)


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)


n = int(input())
CF = {}
for i in range(n):
	x = list(map(int, input().split()))
	CF[x[0]] = x[1]
m = int(input())
TC = {}
for i in range(m):
	x = list(map(int, input().split()))
	TC[x[0]] = x[1]
keys_Tc = set(TC.keys())
keys_Cf = set(CF.keys())
common = keys_Cf & keys_Tc
s = 0
for x in TC:
	if x not in common:
		s += TC[x]
for x in CF:
	if x not in common:
		s += CF[x]
for x in common:
	s += max(CF[x], TC[x])
print(s)


def main():
	dict = {}
	for j in range(2):
		n = int(input())
		for i in range(n):
			(x, y) = map(int, input().split())
			if x in dict:
				dict[x] = max(dict[x], y)
			else:
				dict[x] = y
	print(sum(dict.values()))
main()


import math
import itertools
import collections

def getdict(n):
	d = {}
	if type(n) is list or type(n) is str:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def wr(arr):
	return '\n'.join(map(str, arr))

def revn(n):
	return int(str(n)[::-1])

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True
n = ii()
d = {}
for i in range(n):
	(a, x) = mi()
	d[a] = x
m = ii()
for i in range(m):
	(b, y) = mi()
	if b in d:
		d[b] = max(d[b], y)
	else:
		d[b] = y
ans = 0
for i in d:
	ans += d[i]
print(ans)


I = lambda : map(int, input().split())
t = {}
for _ in range(int(input())):
	(a, x) = I()
	t[a] = x
for _ in range(int(input())):
	(b, y) = I()
	if b in t:
		t[b] = max(t[b], y)
	else:
		t[b] = y
print(sum(t.values()))


from bisect import bisect_left as bs
(d1, d2) = ({}, {})
total = 0
for _ in range(int(input().strip())):
	(x, y) = [int(i) for i in input().strip().split()]
	total += y
	d1[x] = y
for _ in range(int(input().strip())):
	(x, y) = [int(i) for i in input().strip().split()]
	total += y
	d2[x] = y
for i in d1:
	if i in d2:
		total -= min(d1[i], d2[i])
print(total)


n = int(input())
mydict = dict()
for i in range(n):
	(key, value) = [int(k) for k in input().split()]
	mydict[key] = value
m = int(input())
for i in range(m):
	(key, value) = [int(k) for k in input().split()]
	if key in mydict:
		mydict[key] = max(mydict[key], value)
	else:
		mydict[key] = value
print(sum(mydict.values()))


n = int(input())
prices = {}
total = 0
for i in range(n):
	(a, x) = tuple(map(int, input().split()))
	prices[a] = x
	total += x
m = int(input())
for j in range(m):
	(b, y) = tuple(map(int, input().split()))
	if b in prices and prices[b] < y:
		total -= prices[b]
		total += y
	elif b not in prices:
		total += y
print(total)


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


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()


n = int(input())
a = []
for i in range(n):
	a.append([])
for i in range(n):
	(ai, xi) = map(int, input().split())
	a[i].append(ai)
	a[i].append(xi)
b = []
m = int(input())
for i in range(m):
	b.append([])
for i in range(m):
	(bi, yi) = map(int, input().split())
	b[i].append(bi)
	b[i].append(yi)
a = sorted(a)
b = sorted(b)
common1 = []
common2 = []
other = []
i = 0
j = 0
while i < n and j < m:
	if a[i][0] == b[j][0]:
		common1.append(a[i])
		common2.append(b[j])
		i += 1
		j += 1
	elif a[i][0] > b[j][0]:
		other.append(b[j])
		j += 1
	else:
		other.append(a[i])
		i += 1
while i < n:
	other.append(a[i])
	i += 1
while j < m:
	other.append(b[j])
	j += 1
ans = 0
for i in range(len(other)):
	ans += other[i][1]
for i in range(len(common1)):
	if common1[i][1] > common2[i][1]:
		ans += common1[i][1]
	else:
		ans += common2[i][1]
print(ans)


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 d.keys():
		result += d[key]
	return result
print(solve())


o = {}
for _ in range(2):
	for i in range(int(input())):
		(a, x) = map(int, input().strip().split())
		o[a] = max(o.get(a, 0), x)
print(sum(o.values()))


m = int(input())
a = []
for i in range(m):
	t = (r, s) = [t(s) for (t, s) in zip((int, int), input().split())]
	a.append(t)
n = int(input())
b = []
for i in range(n):
	t = (r, s) = [t(s) for (t, s) in zip((int, int), input().split())]
	b.append(t)
al = sorted(a + b, key=lambda x: x[0])
acc = 0
no = -1
pm = -1
for i in al:
	if i[0] != no:
		no = i[0]
		pm = i[1]
		acc += i[1]
	elif i[1] > pm:
		acc += -pm + i[1]
		pm = i[1]
print(acc)


n = int(input())
ch = {}
e = set()
for i in range(n):
	(k, v) = input().split()
	ch[k] = int(v)
	e.add(k)
m = int(input())
th = {}
for i in range(m):
	(k, v) = input().split()
	th[k] = int(v)
	e.add(k)
ans = 0
for i in e:
	if i in th:
		if i in ch:
			ans += max(th.get(i), ch.get(i))
		else:
			ans += th.get(i)
	else:
		ans += ch.get(i)
print(ans)


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 = {}
for i in range(n):
	(a, b) = map(int, input().split())
	d[a] = b
m = int(input())
for i in range(m):
	(a, b) = map(int, input().split())
	if a in d.keys():
		d[a] = max(d[a], b)
	else:
		d[a] = b
print(sum(d.values()))


from collections import defaultdict as dd, deque
E = set()
n = int(input())
C1 = dd(int)
for _ in range(n):
	(i, c) = map(int, input().split())
	C1[i] = c
	E.add(i)
m = int(input())
C2 = dd(int)
for _ in range(m):
	(i, c) = map(int, input().split())
	C2[i] = c
	E.add(i)
e = 0
for el in E:
	e += max(C1[el], C2[el])
print(e)


n = int(input())
a = {}
for i in range(n):
	x = list(map(int, input().split()))
	a[x[0]] = x[1]
m = int(input())
b = {}
for j in range(m):
	x = list(map(int, input().split()))
	b[x[0]] = x[1]
ans = 0
for element in a:
	try:
		if a[element] > b[element]:
			ans += a[element]
	except KeyError:
		ans += a[element]
for element in b:
	try:
		if b[element] >= a[element]:
			ans += b[element]
	except KeyError:
		ans += b[element]
print(ans)


n = int(input())
dic1 = dict()
for i in range(n):
	(a, b) = map(int, input().split())
	dic1[a] = b
n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	if a in dic1:
		if b > dic1[a]:
			dic1[a] = b
	else:
		dic1[a] = b
sum = 0
for i in dic1:
	sum += dic1[i]
print(sum)


n = int(input())
ans = {}
for i in range(n):
	(a, x) = map(int, input().split())
	ans[a] = x
m = int(input())
for i in range(m):
	(a, x) = 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)


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)))


import sys
import filecmp
import math
FILE_IO = False
if FILE_IO:
	input_stream = open('input_test.txt')
	sys.stdout = open('current_output2.txt', 'w')
else:
	input_stream = sys.stdin
map = {}
n = int(input_stream.readline())
for _ in range(n):
	input_list = input_stream.readline().split()
	(a, x) = input_list
	map[int(a)] = int(x)
m = int(input_stream.readline())
for _ in range(m):
	input_list = input_stream.readline().split()
	(b, y) = input_list
	exists = map.get(int(b))
	if exists is not None:
		if exists < int(y):
			map[int(b)] = int(y)
	else:
		map[int(b)] = int(y)
sum = 0
for (key, val) in map.items():
	sum += val
print(sum)


profit = dict()
n = int(input())
for i in range(n):
	(x, p) = [int(j) for j in input().split()]
	profit[x] = p
n = int(input())
for i in range(n):
	(x, p) = [int(j) for j in input().split()]
	if x in profit:
		profit[x] = max(profit[x], p)
	else:
		profit[x] = p
ans = 0
for i in profit.values():
	ans += i
print(ans)


def maxInt(a, b):
	if a > b:
		return a
	else:
		return b
n = int(input())
maxMap = {}
for i in range(n):
	(a, b) = map(int, input().split())
	maxMap[a] = b
m = int(input())
for j in range(m):
	(a, b) = map(int, input().split())
	if a in maxMap:
		maxMap[a] = maxInt(maxMap[a], b)
	else:
		maxMap[a] = b
sol = 0
for (key, value) in maxMap.items():
	sol += value
print(sol)


n = int(input())
dict1 = {}
for i in range(n):
	(a, x) = map(int, input().split())
	dict1[a] = [x]
m = int(input())
for i in range(m):
	(a, x) = map(int, input().split())
	try:
		dict1[a].append(x)
	except:
		KeyError
		dict1[a] = [x]
ans = 0
for i in dict1:
	ans += max(dict1[i])
print(ans)


from collections import defaultdict
d = defaultdict(int)
n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	d[a] = b
m = int(input())
for i in range(m):
	(a, b) = map(int, input().split())
	if d[a] < b:
		d[a] = b
print(sum(d.values()))


from operator import itemgetter
n = int(input())
nlist = []
for i in range(n):
	inp = input().split()
	nlist.append([int(inp[0]), int(inp[1])])
m = int(input())
mlist = []
for i in range(m):
	inp = input().split()
	mlist.append([int(inp[0]), int(inp[1])])
total = sorted(nlist + mlist, key=itemgetter(0))
current = -1
maxi = 0
res = 0
for i in range(n + m):
	if current == total[i][0]:
		if total[i][1] > maxi:
			res = res - maxi
			res = res + total[i][1]
			maxi = total[i][1]
	else:
		res = res + total[i][1]
		current = total[i][0]
		maxi = total[i][1]
print(res)


import sys

def main():
	s = sys.stdin.readlines()
	n = int(s[0])
	s = [list(map(int, i.split())) for i in s]
	k = {int(s[i][0]): int(s[i][1]) for i in range(1, n + 1)}
	for i in range(n + 2, len(s)):
		if s[i][0] not in k:
			k[s[i][0]] = s[i][1]
		else:
			k[s[i][0]] = max(k[s[i][0]], s[i][1])
	print(sum([k[x] for x in k]))
main()


from collections import defaultdict
costs = defaultdict(list)
nos_cf = int(input())
for i in range(nos_cf):
	(i, c) = [int(x) for x in input().split()]
	costs[i].append(c)
nos_tf = int(input())
for i in range(nos_tf):
	(i, c) = [int(x) for x in input().split()]
	costs[i].append(c)
ans = 0
for (i, v) in costs.items():
	ans += max(v)
print(ans)


def code4():
	n = int(input())
	x = 0
	p = {}
	for i in range(n):
		(a, b) = map(int, input().split())
		p[a] = b
	m = int(input())
	for i in range(m):
		(a, b) = map(int, input().split())
		if a not in p:
			x += b
		else:
			p[a] = max(p[a], b)
	x += sum(p.values())
	print(x)
code4()


n = int(input())
elements1 = {}
prices = []
for i in range(n):
	data = input().split(' ')
	elements1[int(data[0])] = int(data[1])
m = int(input())
sum = 0
elements2 = set()
for i in range(m):
	data = input().split(' ')
	e = int(data[0])
	p = int(data[1])
	elements2.add(e)
	if e in elements1:
		sum += max(elements1[e], p)
	else:
		sum += p
for e in elements1:
	if not e in elements2:
		sum += elements1[e]
print(sum)


n = int(input())
arr = []
for _ in range(n):
	arr.append(list(map(int, input().split())))
m = int(input())
for _ in range(m):
	arr.append(list(map(int, input().split())))
arr.sort(key=lambda x: x[0])
val = 0
i = 0
while i < len(arr) - 1:
	if arr[i][0] == arr[i + 1][0]:
		val += max(arr[i][1], arr[i + 1][1])
		i += 2
	else:
		val += arr[i][1]
		i += 1
if len(arr) > 1:
	if arr[len(arr) - 1][0] != arr[len(arr) - 2][0]:
		val += arr[len(arr) - 1][1]
else:
	val += arr[0][1]
print(val)


n = int(input())
(sum1, sum2) = (0, 0)
a = dict()
for i in range(n):
	s = list(map(int, input().split()))
	a[s[0]] = s[1]
	sum1 += s[1]
m = int(input())
b = dict()
for i in range(m):
	s = list(map(int, input().split()))
	b[s[0]] = s[1]
	sum2 += s[1]
c = set(a.keys()).intersection(set(b.keys()))
for i in c:
	if a[i] > b[i]:
		sum2 -= b[i]
	else:
		sum1 -= a[i]
print(sum1 + sum2)


from math import *

def main():
	Map = {}
	n = int(input())
	for i in range(n):
		(a, b) = map(int, input().split())
		Map[a] = b
	m = int(input())
	for i in range(m):
		(a, b) = map(int, input().split())
		if a in Map:
			Map[a] = max(Map[a], b)
		else:
			Map[a] = b
	ans = 0
	for i in Map:
		ans += Map[i]
	print(ans)
try:
	while True:
		main()
except EOFError:
	pass


n1 = int(input())
cf = {}
result = 0
for i in range(0, n1):
	str_in = input()
	num = [int(n) for n in str_in.split()]
	cf[num[0]] = num[1]
	result += num[1]
n2 = int(input())
for i in range(0, n2):
	str_in = input()
	num = [int(n) for n in str_in.split()]
	if num[0] in cf:
		if num[1] > cf[num[0]]:
			result = result + num[1] - cf[num[0]]
		del cf[num[0]]
		continue
	result += num[1]
print(result)


fast = lambda : stdin.readline().strip()
zzz = lambda : [int(i) for i in fast().split()]
(z, zz) = (input, lambda : list(map(int, z().split())))
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from re import *
from sys import *
from math import *
from heapq import *
from queue import *
from bisect import *
from string import *
from itertools import *
from collections import *
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from collections import Counter as cc
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def output(answer):
	stdout.write(str(answer))
lst1 = {}
lst2 = {}
n = int(z())
lst = set()
for _ in range(n):
	(x, y) = zzz()
	lst1[x] = y
	lst.add(x)
m = int(z())
for _ in range(m):
	(x, y) = zzz()
	lst2[x] = y
	lst.add(x)
ans = 0
for i in lst:
	try:
		x = lst1[i]
	except:
		x = 0
	try:
		y = lst2[i]
	except:
		y = 0
	ans += max(x, y)
print(ans)


d = {}
n = int(input())
for i in range(0, n):
	ins = input()
	numbers = [int(n) for n in ins.split()]
	a = numbers[0]
	b = numbers[1]
	d[a] = b
n = int(input())
for i in range(0, n):
	ins = input()
	number = [int(n) for n in ins.split()]
	a = number[0]
	b = number[1]
	if a in d:
		if d[a] < b:
			d[a] = b
	else:
		d[a] = b
sum = 0
for (key, value) in d.items():
	sum += value
print(sum)


n = int(input())
d = dict()
for i in range(n):
	(x, y) = map(int, input().split())
	d[x] = y
m = int(input())
for i in range(m):
	(x, y) = map(int, input().split())
	if x in d:
		d[x] = max(y, d.get(x))
	else:
		d[x] = y
print(sum(d.values()))


n = int(input())
a = dict()
sum = 0
for i in range(n):
	(t1, t2) = [int(i) for i in input().split()]
	a[t1] = t2
	sum += t2
m = int(input())
for j in range(m):
	(t1, t2) = [int(i) for i in input().split()]
	if a.get(t1) == None:
		sum += t2
	elif a[t1] < t2:
		sum = sum - a[t1] + t2
print(sum)


n = int(input())
a = {}
for i in range(n):
	(p, q) = map(int, input().split())
	a[p] = q
m = int(input())
for j in range(m):
	(p, q) = map(int, input().split())
	if p in a and a[p] < q:
		a[p] = q
	elif p not in a:
		a[p] = q
print(sum(a.values()))


n = int(input())
d = {}
s = set()
for i in range(n):
	(k, k1) = map(int, input().split())
	d[k] = k1
	s.add(k)
m = int(input())
for j in range(m):
	(k, k1) = map(int, input().split())
	if k in s:
		if d.get(k) < k1:
			d[k] = k1
	else:
		d[k] = k1
summ = 0
for key in d:
	summ += d[key]
print(summ)


valueSum = 0
used = {}
aNumElements = int(input())
for x in range(aNumElements):
	line = input()
	elem = int(line.split(' ')[0])
	price = int(line.split(' ')[1])
	valueSum += price
	used[elem] = price
bNumElements = int(input())
for y in range(bNumElements):
	line = input()
	elem = int(line.split(' ')[0])
	price = int(line.split(' ')[1])
	if elem in used:
		if used[elem] < price:
			valueSum += price - used[elem]
	else:
		valueSum += price
print(valueSum)


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()))


e = {}
n = int(input())
sum = 0
for i in range(n):
	(x, price) = [int(x) for x in input().split()]
	e[x] = price
	sum += price
m = int(input())
for i in range(m):
	(x, price) = [int(x) for x in input().split()]
	a = e.get(x, False)
	if a:
		if e[x] < price:
			sum -= e[x]
			sum += price
	else:
		sum += price
print(sum)


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():
	ss += v
print(ss)


n = int(input())
d = {}
for i in range(n):
	(p, q) = map(int, input().split())
	if p in d.keys():
		if d[p] < q:
			d[p] = q
	else:
		d[p] = q
m = int(input())
for i in range(m):
	(p, q) = map(int, input().split())
	if p in d.keys():
		if d[p] < q:
			d[p] = q
	else:
		d[p] = q
sum = 0
for i in d.keys():
	sum += d[i]
print(sum)


n = int(input())
ax = [list(map(int, input().split())) for _ in [0] * n]
m = int(input())
by = [list(map(int, input().split())) for _ in [0] * m]
r = {}
for (a, x) in ax:
	r[a] = x
for (b, y) in by:
	if b in r.keys():
		r[b] = max(r[b], y)
	else:
		r[b] = y
print(sum(r.values()))


elements1 = {}
elements2 = {}
n = int(input())
for i in range(n):
	loc = list(map(int, input().split()))
	elements1[loc[0]] = loc[1]
m = int(input())
for j in range(m):
	loc = list(map(int, input().split()))
	elements2[loc[0]] = loc[1]
items1 = elements1
items2 = elements2
keys1 = set(list(elements1.keys()))
keys2 = set(list(elements2.keys()))
intersection = list(keys1.intersection(keys2))
ost = {}
for i in range(len(intersection)):
	if elements1[intersection[i]] > elements2[intersection[i]]:
		elements1[intersection[i]] = items1[intersection[i]]
		elements2[intersection[i]] = 0
	else:
		elements2[intersection[i]] = items2[intersection[i]]
		elements1[intersection[i]] = 0
print(sum(list(elements1.values())) + sum(list(elements2.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()


n = int(input())
k = {}
su = 0
for i in range(n):
	(a, b) = map(int, input().split())
	k[a] = b
m = int(input())
for i in range(m):
	(a, b) = map(int, input().split())
	if k.get(a) == None:
		k[a] = b
	elif k[a] < b:
		k[a] = b
print(sum(k.values()))


from collections import defaultdict

class CodeforcesTask981BSolution:

	def __init__(self):
		self.result = ''
		self.n = 0
		self.first = []
		self.m = 0
		self.second = []

	def read_input(self):
		self.n = int(input())
		for _ in range(self.n):
			self.first.append([int(y) for y in input().split(' ')])
		self.m = int(input())
		for _ in range(self.m):
			self.second.append([int(y) for y in input().split(' ')])

	def process_task(self):
		top_income = defaultdict(int)
		income = 0
		for a in self.first + self.second:
			top_income[a[0]] = max(top_income[a[0]], a[1])
		income = sum(top_income.values())
		self.result = str(income)

	def get_result(self):
		return self.result
Solution = CodeforcesTask981BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


def main():
	dct = {}
	n = int(input().strip())
	for _ in range(n):
		(a, x) = map(int, input().strip().split())
		dct[a] = x
	m = int(input().strip())
	for _ in range(m):
		(b, y) = map(int, input().strip().split())
		if b not in dct or (b in dct and y > dct[b]):
			dct[b] = y
	result = sum((v for v in dct.values()))
	print(result)
main()


def BusinessmenProblems(uniqueSet, chemForcses, topChemist):
	totalIncome = 0
	for s in uniqueSet:
		x = 0
		y = 0
		if s in chemForcses:
			x = chemForcses[s]
		if s in topChemist:
			y = topChemist[s]
		if x > y:
			totalIncome += x
		else:
			totalIncome += y
	return totalIncome
n = int(input())
uniqueSet = set()
chemForcses = {}
i = 0
while i < n:
	(a, x) = [int(l) for l in input().split()]
	uniqueSet.add(a)
	chemForcses[a] = x
	i += 1
m = int(input())
topChemist = {}
i = 0
while i < m:
	(a, x) = [int(l) for l in input().split()]
	uniqueSet.add(a)
	topChemist[a] = x
	i += 1
print(BusinessmenProblems(uniqueSet, chemForcses, topChemist))


import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_int_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())
cf = {}
tc = {}
n = get_int()
for i in range(n):
	(a, b) = get_int_list()
	cf[a] = b
n = get_int()
for i in range(n):
	(a, b) = get_int_list()
	tc[a] = b
s = set(cf.keys()).union(set(tc.keys()))
total = 0
for i in s:
	if i in cf and i in tc:
		if cf[i] > tc[i]:
			total += cf[i]
		else:
			total += tc[i]
	elif i in cf:
		total += cf[i]
	else:
		total += tc[i]
print(total)


n = int(input())
l = list()
for i in range(n):
	(a, x) = map(int, input().split())
	l.append((a, x))
m = int(input())
for i in range(m):
	(b, y) = map(int, input().split())
	l.append((b, y))
l = sorted(l, key=lambda l: l[0], reverse=False)
prev = l[0][0]
s = 0
max = l[0][1]
k = len(l)
for i in range(1, k):
	if l[i][0] == prev:
		if max < l[i][1]:
			max = l[i][1]
	else:
		s += max
		max = l[i][1]
	prev = l[i][0]
s += max
print(s)


import sys
n = int(sys.stdin.readline().strip())
d = {}
for _ in range(n):
	(ai, xi) = map(int, sys.stdin.readline().strip().split(' '))
	if ai in d:
		d[ai] = max(d[ai], xi)
	else:
		d[ai] = xi
m = int(sys.stdin.readline().strip())
for _ in range(m):
	(ai, xi) = map(int, sys.stdin.readline().strip().split(' '))
	if ai in d:
		d[ai] = max(d[ai], xi)
	else:
		d[ai] = xi
ans = 0
for (ai, xi) in d.items():
	ans += xi
print(ans)


n = int(input())
count = 0
s1 = dict()
for x in range(n):
	a = input().split()
	s1[int(a[0])] = int(a[1])
m = int(input())
for x in range(m):
	a = input().split()
	if int(a[0]) in s1.keys():
		if int(a[1]) > s1[int(a[0])]:
			count += int(a[1])
		else:
			count += s1[int(a[0])]
		del s1[int(a[0])]
	else:
		count += int(a[1])
for i in s1.values():
	count += i
print(count)


a_m = {}
ret = 0
n = int(input())
for i in range(n):
	(a, v) = list(map(int, input().strip().split()))
	a_m[a] = v
	ret += v
m = int(input())
for j in range(m):
	(b, v) = list(map(int, input().split()))
	if b in a_m:
		if v > a_m[b]:
			ret += v - a_m[b]
	else:
		ret += v
print(ret)


n1 = int(input())
l1 = [[int(i) for i in input().split()] for i in range(n1)]
n2 = int(input())
l2 = [[int(i) for i in input().split()] for i in range(n2)]
dic1 = {l1[i][0]: l1[i][1] for i in range(n1)}
dic2 = {l2[i][0]: l2[i][1] for i in range(n2)}
s = 0
for i in dic1:
	if i in dic2.keys():
		if dic1.get(i) >= dic2.get(i):
			s += dic1.get(i)
			dic2[i] = 0
		elif dic1.get(i) < dic2.get(i):
			s += dic2.get(i)
			dic1[i] = 0
print(sum(list(dic1.values()) + list(dic2.values())))


first_line = list(map(int, input().split()))
n = first_line[0]
chem_f_tuple = []
for i in list(range(n)):
	line = list(map(int, input().split()))
	chem_f_tuple.append((line[0], line[1]))
first_line = list(map(int, input().split()))
m = first_line[0]
for i in list(range(m)):
	line = list(map(int, input().split()))
	chem_f_tuple.append((line[0], line[1]))
chem_f_tuple.sort(reverse=True)
previous_number = 0
ans = 0
for (a, b) in chem_f_tuple:
	if a != previous_number:
		ans += b
	previous_number = a
print(ans)


from collections import defaultdict
d = defaultdict(int)
for _ in range(int(input())):
	(a, x) = map(int, input().split())
	d[a] = x
for _ in range(int(input())):
	(b, y) = map(int, input().split())
	d[b] = max(d[b], y)
print(sum(d.values()))


n1 = int(input())
item_pair = {}
for i in range(0, n1):
	item_price = input().split()
	item = item_price[0]
	price = item_price[1]
	item_pair[item] = int(price)
n2 = int(input())
for i in range(0, n2):
	item_price = input().split()
	item = item_price[0]
	price = int(item_price[1])
	if item in item_pair:
		n1_price = item_pair[item]
		if price > n1_price:
			item_pair[item] = price
	else:
		item_pair[item] = price
sum = 0
for value in item_pair.values():
	sum += value
print(sum)


n = int(input())
a = {}
b = {}
for i in range(n):
	(x, y) = map(int, input().split())
	a[x] = y
m = int(input())
for i in range(m):
	(x, y) = map(int, input().split())
	if x in a.keys():
		if y >= a[x]:
			a[x] = y
	else:
		a[x] = y
s = 0
s = sum(a.values())
print(s)


n = int(input())
A = {}
ans = 0
for i in range(n):
	(a, b) = input().split()
	(a, b) = [int(a), int(b)]
	A[a] = b
	ans = ans + b
m = int(input())
for i in range(m):
	(a, b) = input().split()
	(a, b) = [int(a), int(b)]
	if A.get(a) != None:
		ans -= A[a]
		ans += max(A[a], b)
	else:
		ans += b
print(ans)


d = {}
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	d[a] = b
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	if a in d.keys():
		if d[a] < b:
			d[a] = b
	else:
		d[a] = b
print(sum(d.values()))


n = int(input())
a = [[int(x) for x in input().split()] for i in range(0, n)]
m = int(input())
b = [[int(x) for x in input().split()] for i in range(0, m)]
a = sorted(a, key=lambda x: x[0])
b = sorted(b, key=lambda x: x[0])
s = 0
i = 0
j = 0
while i < n and j < m:
	if a[i][0] < b[j][0]:
		s = s + a[i][1]
		i += 1
	elif a[i][0] > b[j][0]:
		s = s + b[j][1]
		j += 1
	else:
		s = s + max(a[i][1], b[j][1])
		i += 1
		j += 1
if i == n:
	for k in range(j, m):
		s = s + b[k][1]
else:
	for p in range(i, n):
		s = s + a[p][1]
print(s)


from collections import defaultdict
t = 2
s = 0
dic = defaultdict(lambda : [])
while t > 0:
	n = int(input())
	for i in range(n):
		(a, x) = map(int, input().split())
		dic[a].append(x)
	t = t - 1
for (keys, values) in dic.items():
	p = max(values)
	s += p
print(s)


def main():
	d = {}
	n = int(input())
	answer = 0
	for _ in range(n):
		(a, b) = [int(x) for x in input().split()]
		d[a] = b
		answer += b
	m = int(input())
	for _ in range(m):
		(a, b) = [int(x) for x in input().split()]
		if a in d:
			if b > d[a]:
				answer += b - d[a]
		else:
			answer += b
	print(answer)
main()


def prog():
	from sys import stdin
	n = int(stdin.readline())
	di = {}
	res = 0
	for i in range(n):
		(a, x) = map(int, stdin.readline().split())
		di[a] = x
		res += x
	m = int(stdin.readline())
	for i in range(m):
		(b, y) = map(int, stdin.readline().split())
		res += y
		if di.get(b) != None:
			res -= min(y, di[b])
	print(res)
prog()


profits = dict()
for x in range(2):
	n = int(input())
	for i in range(n):
		(iD, profit) = map(int, input().split())
		if iD in profits:
			profits[iD] = max(profits[iD], profit)
		else:
			profits[iD] = profit
print(sum([b for (a, b) in profits.items()]))


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)


n = int(input())
dic = {}
for i in range(n):
	(key, value) = map(int, input().split())
	dic[key] = value
m = int(input())
for i in range(m):
	(key, value) = map(int, input().split())
	if key in dic.keys():
		dic[key] = max(dic[key], value)
	else:
		dic[key] = value
s = 0
for val in dic.keys():
	s += dic[val]
print(s)


n = int(input())
a = {}
b = {}
a_sum = 0
b_sum = 0
for i in range(0, n):
	s = input().split()
	a[s[0]] = int(s[1])
	a_sum += int(s[1])
m = int(input())
for i in range(0, m):
	s = input().split()
	b[s[0]] = int(s[1])
	b_sum += int(s[1])
intersect = set(a.keys()).intersection(b.keys())
for el in intersect:
	if a[el] > b[el]:
		b_sum = b_sum - b[el]
	elif b[el] > a[el]:
		a_sum = a_sum - a[el]
	elif a[el] == b[el]:
		a_sum = a_sum - a[el]
print(a_sum + b_sum)


entrada = int(input())
arreglo = {}
for i in range(entrada):
	(x, y) = [int(x) for x in input().split()]
	arreglo[x] = y
entrada2 = int(input())
for i in range(entrada2):
	(x, y) = [int(x) for x in input().split()]
	if x in arreglo:
		y2 = arreglo[x]
		arreglo[x] = max(y, y2)
	else:
		arreglo[x] = y
print(sum(arreglo.values()))


n = int(input())
dic = {}
for k in range(n):
	a = [int(q) for q in input().strip().split()]
	dic[a[0]] = a[1]
n = int(input())
for k in range(n):
	a = [int(q) for q in input().strip().split()]
	if a[0] in dic:
		if dic[a[0]] < a[1]:
			dic[a[0]] = a[1]
	else:
		dic[a[0]] = a[1]
sum = 0
for k in dic:
	sum += dic[k]
print(sum)


from collections import defaultdict
n = int(input())
count = defaultdict(int)
price = defaultdict(int)
profit = int
profit = 0
for i in range(n):
	A = input().split()
	k = int(A[0])
	l = int(A[1])
	count[k] = 1
	price[k] = l
m = int(input())
for i in range(m):
	A = input().split()
	k = int(A[0])
	l = int(A[1])
	if l > price[k]:
		count[k] == -1
		price[k] = l
for i in price.keys():
	profit = profit + price[i]
print(profit)


n = int(input())
dic = {}
for i in range(n):
	a = input().split()
	dic[int(a[0])] = int(a[1])
m = int(input())
for j in range(m):
	b = input().split()
	if int(b[0]) in dic.keys():
		dic[int(b[0])] = max(dic[int(b[0])], int(b[1]))
	else:
		dic[int(b[0])] = int(b[1])
print(sum(dic.values()))


import math
import sys

def scan():
	return list(map(int, sys.stdin.readline().strip().split()))

def solution():
	n = int(input())
	a = []
	for i in range(n):
		a.append(scan())
	m = int(input())
	b = []
	a = sorted(a, key=lambda x: x[0])
	b = sorted(a, key=lambda x: x[0])
	for j in range(m):
		b.append(scan())
	c = {}
	for i in a:
		if i[0] in c:
			c[i[0]] = max(c[i[0]], i[1])
		else:
			c[i[0]] = i[1]
	for i in b:
		if i[0] in c:
			c[i[0]] = max(c[i[0]], i[1])
		else:
			c[i[0]] = i[1]
	s = 0
	for i in c:
		s += c[i]
	print(s)
solution()


import sys, math, os.path
FILE_INPUT = 'B.in'
DEBUG = os.path.isfile(FILE_INPUT)
if DEBUG:
	sys.stdin = open(FILE_INPUT)

def ni():
	return map(int, input().split(' '))

def nia():
	return list(map(int, input().split()))

def log(x):
	if DEBUG:
		print(x)
(n,) = ni()
ll = {}
for i in range(n):
	(a, x) = ni()
	ll[a] = x
(m,) = ni()
for j in range(m):
	(b, y) = ni()
	if b in ll:
		ll[b] = max(ll[b], y)
	else:
		ll[b] = y
s = 0
for v in ll.values():
	s += v
print(s)


n = int(input())
d1 = dict()
for i in range(n):
	(c, d) = map(int, input().split())
	d1[c] = d
m = int(input())
d2 = dict()
for i in range(m):
	(c, d) = map(int, input().split())
	d2[c] = d
out = 0
for i in d1:
	if d2.get(i, 0) > 0:
		if d2[i] > d1[i]:
			d1[i] = 0
		else:
			d2[i] = 0
for i in d2:
	if d1.get(i, 0) > 0:
		if d2[i] > d1[i]:
			d1[i] = 0
		else:
			d2[i] = 0
for i in d1:
	out += d1[i]
for i in d2:
	out += d2[i]
print(out)


n = int(input())
d = {}
ans = 0
for i in range(n):
	(curr, cost) = map(int, input().split())
	if d.get(curr) is None:
		d[curr] = cost
	d[curr] = max(d[curr], cost)
m = int(input())
for i in range(m):
	(curr, cost) = map(int, input().split())
	if d.get(curr) is None:
		d[curr] = cost
	d[curr] = max(d[curr], cost)
for el in d.values():
	ans += el
print(ans)


from collections import defaultdict as dd

def solve():
	C = dd(int)
	N = int(input())
	for n in range(N):
		(k, v) = map(int, input().split())
		C[k] = v
	M = int(input())
	for m in range(M):
		(k, v) = map(int, input().split())
		C[k] = max(v, C[k])
	print(sum(C.values()))
try:
	local_input()
except:
	pass
solve()


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())
k = 0
d = {}
for i in range(n):
	(a, b) = map(int, input().split())
	d[a] = b
m = int(input())
for i in range(m):
	(a, b) = map(int, input().split())
	d[a] = max(d.get(a, 0), b)
for x in d:
	k += d[x]
print(k)


cost = 0
n = int(input())
nn = {}
for i in range(n):
	(x, y) = map(int, input().split())
	nn[x] = y
	cost += y
m = int(input())
mm = {}
for i in range(m):
	(x, y) = map(int, input().split())
	mm[x] = y
	cost += y
same = list(set(list(nn.keys())) & set(list(mm.keys())))
for i in same:
	if nn[i] > mm[i]:
		cost -= mm[i]
	else:
		cost -= nn[i]
print(cost)


from collections import defaultdict
elements = defaultdict(int)
n = int(input())
for i in range(n):
	(a, x) = [int(num) for num in input().split(' ')]
	if elements[a] < x:
		elements[a] = x
m = int(input())
for i in range(m):
	(b, y) = [int(num) for num in input().split(' ')]
	if elements[b] < y:
		elements[b] = y
print(sum(elements.values()))


from copy import deepcopy
import itertools
from bisect import bisect_left

def read():
	return int(input())

def readmap():
	return map(int, input().split())

def readlist():
	return list(map(int, input().split()))
N = read()
A = []
for _ in range(N):
	(a, x) = readmap()
	A.append((a, x))
M = read()
B = []
for _ in range(M):
	(b, y) = readmap()
	B.append((b, y))
A.sort(key=lambda z: z[0])
Anew = []
for i in range(N):
	Anew.append(A[i][0])
Xnew = []
for i in range(N):
	Xnew.append(A[i][1])
ans = 0
for i in range(N):
	ans += A[i][1]
for i in range(M):
	b = B[i][0]
	idx = bisect_left(Anew, b)
	if idx == N:
		ans += B[i][1]
	elif b == Anew[idx]:
		maxx = max(Xnew[idx], B[i][1])
		ans = ans - Xnew[idx] + maxx
	else:
		ans += B[i][1]
print(ans)


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
l = d.values()
print(sum(l))


TN = 1

def solution():
	n = int(input())
	lst = []
	ax = {}
	for i in range(n):
		(ai, x) = map(int, input().split())
		lst.append(ai)
		ax[ai] = x
	m = int(input())
	by = {}
	for i in range(m):
		(bi, y) = map(int, input().split())
		lst.append(bi)
		by[bi] = y
	lst = set(lst)
	c = 0
	lst = list(lst)
	for i in lst:
		try:
			by[i]
		except:
			c += ax[i]
		else:
			try:
				ax[i]
			except:
				c += by[i]
			else:
				c += max(ax[i], by[i])
	print(c)
while TN != 0:
	solution()
	TN -= 1


n = int(input())
a = list()
for i in range(0, n):
	(x, y) = input().split()
	a.append((int(x), int(y)))
da = dict()
for (x, y) in a:
	da[x] = y
n = int(input())
b = list()
for i in range(0, n):
	(x, y) = input().split()
	b.append((int(x), int(y)))
db = dict()
for (x, y) in b:
	db[x] = y
res = 0
for (k, v) in a:
	if da.get(k, 0) > db.get(k, 0):
		res += da.get(k, 0)
	else:
		res += db.get(k, 0)
	da[k] = 0
	db[k] = 0
for (k, v) in da.items():
	res += v
for (k, v) in db.items():
	res += v
print(res)


Input = lambda : map(int, input().split())
Tmp = dict()
n = int(input())
for i in range(n):
	(a, x) = Input()
	Tmp[a] = x
ans = 0
m = int(input())
for i in range(m):
	(b, x) = Input()
	if Tmp.get(b, -1) == -1:
		Tmp[b] = x
	else:
		ans += max(Tmp[b], x)
		Tmp[b] = 0
for i in Tmp.values():
	ans += i
print(ans)


def business_men():
	teamA = int(input())
	teamA_days = []
	teamA_dic = {}
	for i in range(teamA):
		teamA_days.append(list(map(int, input().split())))
		teamA_dic[teamA_days[i][0]] = teamA_days[i][1]
	teamB = int(input())
	teamB_days = []
	teamB_dic = {}
	for i in range(teamB):
		teamB_days.append(list(map(int, input().split())))
		teamB_dic[teamB_days[i][0]] = teamB_days[i][1]
	output = 0
	checked = {}
	for i in range(len(teamA_days)):
		if teamA_days[i][0] in teamB_dic:
			maximum = max(teamA_dic[teamA_days[i][0]], teamB_dic[teamA_days[i][0]])
			output += maximum
			teamA_dic.pop(teamA_days[i][0])
			teamB_dic.pop(teamA_days[i][0])
			checked[teamA_days[i][0]] = 1
		else:
			output += teamA_days[i][1]
	for i in range(len(teamB_days)):
		if teamB_days[i][0] not in checked:
			output += teamB_days[i][1]
	return output
A = business_men()
print(A)


from collections import defaultdict
n = int(input())
l = defaultdict(list)
m = defaultdict(list)
x = []
for _ in range(n):
	(a, k) = map(int, input().split())
	l[a].append(k)
	m[a].append(-1)
	x.append(a)
z = int(input())
for _ in range(z):
	(a, k) = map(int, input().split())
	m[a].append(k)
	l[a].append(-1)
	x.append(a)
x = list(set(x))
ans = 0
for i in x:
	ans += max(max(l[i]), max(m[i]))
print(ans)


from sys import stdin
input = stdin.readline
n = int(input())
(a, s) = ([], set())
ans = 0
for i in range(n):
	(j, x) = [int(i) for i in input().split()]
	a.append((x, j))
m = int(input())
for i in range(m):
	(j, x) = [int(i) for i in input().split()]
	a.append((x, j))
a.sort(reverse=True)
for t in a:
	if t[1] not in s:
		s.add(t[1])
		ans += t[0]
print(ans)


na = int(input())
a = {}
for i in range(na):
	(x, y) = input().split()
	a[int(x)] = int(y)
nb = int(input())
b = {}
for i in range(nb):
	(x, y) = input().split()
	b[int(x)] = int(y)
t = 0
for (k, v) in a.items():
	if k in b:
		t += max([b[k], v])
	else:
		t += v
for (k, v) in b.items():
	if not k in a:
		t += v
print(t)


n = int(input())
cmn = {}
cmnn = []
a = {}
for i in range(n):
	(ai, xi) = map(int, input().split())
	cmn[ai] = 1
	a[ai] = xi
m = int(input())
b = {}
for i in range(m):
	(bi, xi) = map(int, input().split())
	b[bi] = xi
	if cmn.get(bi, 0) == 1:
		if a[bi] > b[bi]:
			del b[bi]
		else:
			del a[bi]
print(sum(a.values()) + sum(b.values()))


n = int(input())
chemForces = dict()
allSum = 0
for i in range(n):
	newChem = input().split()
	(i, k) = (int(newChem[0]), int(newChem[1]))
	allSum += k
	chemForces.update({i: k})
m = int(input())
topChemist = dict()
for j in range(m):
	newTop = input().split()
	(j, t) = (int(newTop[0]), int(newTop[1]))
	allSum += t
	topChemist.update({j: t})
intersection = set.intersection(set(chemForces), set(topChemist))
for i in intersection:
	chemF = chemForces.get(i)
	topC = topChemist.get(i)
	if chemF <= topC:
		allSum -= chemF
	else:
		allSum -= topC
print(allSum)


N1 = int(input())
busi = {}
for i in range(N1):
	(name, value) = map(int, input().split())
	busi[name] = value
sum = 0
N2 = int(input())
for i in range(N2):
	(name, value) = map(int, input().split())
	if name in busi:
		busi[name] = max(value, busi[name])
	else:
		busi[name] = value
for item in busi:
	sum += busi[item]
print(sum)


n = int(input())
a = {}
b = {}
for _ in range(n):
	(ai, xi) = map(int, input().split())
	a[ai] = xi
m = int(input())
for _ in range(m):
	(bj, yj) = map(int, input().split())
	if bj in a.keys():
		if yj >= a[bj]:
			a[bj] = 0
		else:
			yj = 0
	b[bj] = yj
s = 0
s = sum(a.values()) + sum(b.values())
print(s)


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()))


S = {}
n = int(input())
for i in range(n):
	(a, x) = [int(x) for x in input().split()]
	S[a] = x
m = int(input())
for i in range(m):
	(b, y) = [int(x) for x in input().split()]
	if b in S:
		S[b] = max(S[b], y)
	else:
		S[b] = y
ans = 0
for i in S:
	ans += S[i]
print(ans)


import os
import sys
debug = True
if debug and os.path.exists('input.in'):
	input = open('input.in', 'r').readline
else:
	debug = False
	input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
test_count = 1
if debug:
	test_count = int(input())
for t in range(test_count):
	if debug:
		print('Test Case #', t + 1)
	total = 0
	a_income = {}
	for i in range(inp()):
		(x, y) = invr()
		total += y
		a_income[x] = y
	for _ in range(inp()):
		(x, y) = invr()
		if x in a_income:
			a_y = a_income[x]
			if y > a_y:
				total += y - a_y
		else:
			total += y
	print(total)


n = int(input())
r = []
ans = 0
skip = False
for i in range(n):
	(a, x) = map(int, input().split(' '))
	r.append((a, x))
m = int(input())
for i in range(m):
	(b, y) = map(int, input().split(' '))
	r.append((b, y))
r.sort(key=lambda x: x[0])
r.append((-1, 0))
for i in range(n + m):
	if skip:
		skip = False
		continue
	if r[i][0] == r[i + 1][0]:
		ans += max(r[i][1], r[i + 1][1])
		skip = True
	else:
		ans += r[i][1]
print(ans)


from collections import defaultdict
li1 = defaultdict(int)
n = int(input())
for i in range(n):
	(a, x) = input().split()
	(a, x) = (int(a), int(x))
	li1[a] = x
m = int(input())
for i in range(m):
	(b, y) = input().split()
	(b, y) = (int(b), int(y))
	li1[b] = max(li1[b], y)
print(sum(li1.values()))


n = int(input())
a = []
for i in range(n):
	temp = [int(x) for x in input().split()]
	a += [temp]
m = int(input())
b = []
for i in range(m):
	temp = [int(x) for x in input().split()]
	b += [temp]
s = 0
a.sort()
b.sort()
i = 0
j = 0
while i < n and j < m:
	if a[i][0] == b[j][0]:
		s += max(a[i][1], b[j][1])
		i += 1
		j += 1
	elif a[i][0] < b[j][0]:
		s += a[i][1]
		i += 1
	elif a[i][0] > b[j][0]:
		s += b[j][1]
		j += 1
while i < n:
	s += a[i][1]
	i += 1
while j < m:
	s += b[j][1]
	j += 1
print(s)


(d1, s1, d2, s2) = (dict(), set(), dict(), set())
sum = 0
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	d1[a] = b
	s1.add(a)
	sum += b
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	d2[a] = b
	s2.add(a)
	sum += b
s = s1 & s2
for i in s:
	sum -= min(d1[i], d2[i])
print(sum)


n = int(input())
d = {}
for i in range(n):
	(a, x) = map(int, input().split())
	d[a] = x
m = int(input())
for i in range(m):
	(b, x) = map(int, input().split())
	try:
		d[b] = max(x, d[b])
	except:
		d[b] = x
print(sum(d.values()))


t1 = int(input())
l = []
cost = []
count = 0
data = {}
for i in range(t1):
	(n, k) = map(int, input().strip().split())
	if n not in data:
		count = count + 1
		data.update({n: k})
	elif k > data[n]:
		data[n] = k
t2 = int(input())
for i in range(t2):
	(n, k) = map(int, input().strip().split())
	if n not in data:
		count = count + 1
		data.update({n: k})
	elif k > data[n]:
		data[n] = k
for r in data.keys():
	l.append(data[r])
print(sum(l))


def main():
	dict_elements = {}
	for _ in range(int(input())):
		[element, price] = map(int, input().split())
		dict_elements[element] = price
	for _ in range(int(input())):
		[element, price] = map(int, input().split())
		dict_elements[element] = max(dict_elements.get(element, 0), price)
	print(sum(dict_elements.values()))
main()


def calcMaxIncome(dictA, dictB):
	accumulator = 0
	AKeys = sorted(dictA.keys())
	for i in range(len(AKeys)):
		if AKeys[i] in dictB.keys():
			if dictA[AKeys[i]] > dictB[AKeys[i]]:
				del dictB[AKeys[i]]
			else:
				del dictA[AKeys[i]]
	accumulator = sum(dictA.values()) + sum(dictB.values())
	return accumulator
chemFGoods = {}
topChemGoods = {}
n = int(input())
for _ in range(n):
	(a, x) = map(int, input().rstrip().split())
	chemFGoods[a] = x
m = int(input())
for _ in range(m):
	(b, y) = map(int, input().rstrip().split())
	topChemGoods[b] = y
result = calcMaxIncome(chemFGoods, topChemGoods)
print(result)


n = int(input())
cf = {}
tc = {}
s = set()
ans = 0
for i in range(n):
	(a, b) = map(int, input().split())
	cf[a] = b
	s.add(a)
m = int(input())
for i in range(m):
	(a, b) = map(int, input().split())
	if a in s:
		if cf[a] > b:
			ans += cf[a]
		else:
			ans += b
		s.remove(a)
	else:
		tc[a] = b
for i in s:
	ans += cf[i]
for (q, v) in tc.items():
	ans += v
print(ans)


n = int(input())
a = {k: v for (k, v) in map(lambda line: tuple((int(c) for c in input().split())), range(n))}
m = int(input())
b = {k: v for (k, v) in map(lambda line: tuple((int(c) for c in input().split())), range(m))}
keys = set(a.keys())
keys |= b.keys()

def max_val(k):
	if k in a and k in b:
		return max(a[k], b[k])
	if k in a:
		return a[k]
	return b[k]
print(sum((max_val(k) for k in keys)))


i = int(input())
values = {}
for x in range(i):
	inp = input().split()
	element = inp[0]
	value = int(inp[1])
	values[element] = value
i = int(input())
for x in range(i):
	inp = input().split()
	element = inp[0]
	value = int(inp[1])
	if element in values:
		if values[element] < int(value):
			values[element] = value
	else:
		values[element] = value
print(sum(values.values()))


drugs_a = {}
drugs_b = {}
for _ in range(int(input())):
	(n, p) = map(int, input().split())
	drugs_a[n] = p
for _ in range(int(input())):
	(n, p) = map(int, input().split())
	drugs_b[n] = p
	try:
		if drugs_b[n] > drugs_a[n]:
			del drugs_a[n]
		else:
			del drugs_b[n]
	except KeyError:
		pass
ans = 0
for i in drugs_a.values():
	ans += i
for i in drugs_b.values():
	ans += i
if len(drugs_b) == len(drugs_a):
	temp = min(min(list(drugs_a.values())), min(list(drugs_b.values())))
print(ans)


n = int(input())
m = {}
for i in range(0, n):
	(x, y) = map(int, input().split(' '))
	if m.get(x) == None:
		m.update({x: y})
	else:
		m[x] = max(m.get(x), y)
p = int(input())
for i in range(0, p):
	(x, y) = map(int, input().split(' '))
	if m.get(x) == None:
		m.update({x: y})
	else:
		m[x] = max(m.get(x), y)
print(sum(m.values()))


MyDict = {}
X = int(input())
for i in range(X):
	Temp = list(map(int, input().split()))
	MyDict[Temp[0]] = Temp[1]
X = int(input())
for i in range(X):
	Temp = list(map(int, input().split()))
	MyDict[Temp[0]] = max(Temp[1], MyDict[Temp[0]]) if Temp[0] in MyDict.keys() else Temp[1]
print(sum(MyDict.values()))


res = {}
for _ in range(2):
	n = int(input())
	for i in range(n):
		(a, x) = map(int, input().split(' '))
		res[a] = max(res.get(a, 0), x)
print(sum(res.values()))


r = {}
n = int(input())
for i in range(n):
	(x, y) = map(int, input().split())
	r[x] = y
k = int(input())
for i in range(k):
	(x, y) = map(int, input().split())
	if x in r:
		if r[x] < y:
			r[x] = y
	else:
		r[x] = y
print(sum(r.values()))


sl1 = input()
nt1 = {}
for i in range(int(sl1)):
	(x, y) = input().split()
	nt1[x] = y
sl2 = input()
nt2 = {}
for i in range(int(sl2)):
	(x, y) = input().split()
	nt2[x] = y
rs = 0
for x in nt1:
	if x in nt2 and int(nt2[x]) > int(nt1[x]):
		rs += int(nt2[x])
	else:
		rs += int(nt1[x])
for x in nt2:
	if not x in nt1:
		rs += int(nt2[x])
print(rs)


n = int(input())
ChemForces = {}
for i in range(n):
	temp = input().strip().split(' ')
	ChemForces[temp[0]] = int(temp[1])
m = int(input())
TopChemist = {}
for j in range(m):
	temp = input().strip().split(' ')
	TopChemist[temp[0]] = int(temp[1])
ChemForces_k = set(ChemForces.keys())
TopChemist_k = set(TopChemist.keys())
ChemForces_ku = ChemForces_k.difference(TopChemist_k)
TopChemist_ku = TopChemist_k.difference(ChemForces_k)
ChemTop = ChemForces_k.intersection(TopChemist_k)
sumChemForces_ku = 0
sumTopChemist_ku = 0
sumChemTop = 0
for i in ChemForces_ku:
	sumChemForces_ku += ChemForces[i]
for j in TopChemist_ku:
	sumTopChemist_ku += TopChemist[j]
for k in ChemTop:
	if ChemForces[k] > TopChemist[k]:
		sumChemTop += ChemForces[k]
	else:
		sumChemTop += TopChemist[k]
max_sum = sumChemForces_ku + sumTopChemist_ku + sumChemTop
print(max_sum)


n = int(input())
a = [[int(e) for e in input().split()] for i in range(n)] + [[2000000000, 0]]
m = int(input())
b = [[int(e) for e in input().split()] for i in range(m)] + [[2000000000, 0]]
a.sort()
b.sort()
i1 = 0
i2 = 0
ans = 0
while i1 + i2 < n + m:
	if i1 < n and (i2 >= m or a[i1][0] < b[i2][0]):
		ans += a[i1][1]
		i1 += 1
	elif i1 < n and a[i1][0] == b[i2][0]:
		ans += max(a[i1][1], b[i2][1])
		i1 += 1
		i2 += 1
	else:
		ans += b[i2][1]
		i2 += 1
print(ans)


chem = {}
n = int(input())
for i in range(n):
	(x, y) = map(int, input().split())
	if x in chem:
		chem[x] = max(chem[x], y)
	else:
		chem[x] = y
n = int(input())
for i in range(n):
	(x, y) = map(int, input().split())
	if x in chem:
		chem[x] = max(chem[x], y)
	else:
		chem[x] = y
res = 0
for (_, x) in chem.items():
	res += x
print(res)


inc = {}
for i in range(2):
	n = int(input())
	for i in range(n):
		(a, x) = [int(s) for s in input().split(' ')]
		if a in inc:
			inc[a] = max(inc[a], x)
		else:
			inc[a] = x
print(sum(inc.values()))


n = int(input())
dict = {}
for i in range(n):
	(key, value) = map(int, input().split())
	dict[key] = value
m = int(input())
for i in range(m):
	(key, value) = map(int, input().split())
	if key in dict.keys():
		dict[key] = max(dict[key], value)
	else:
		dict[key] = value
ans = 0
for val in dict.keys():
	ans += dict[val]
print(ans)


a = int(input())
a1 = {}
a2 = {}
s = 0
for i in range(a):
	(q, w) = map(int, input().split())
	a1[q] = w
b = int(input())
for i in range(b):
	(q, w) = map(int, input().split())
	a2[q] = w
for i in a1.keys():
	if i in a2.keys():
		s += max(a1[i], a2[i])
	else:
		s += a1[i]
for i in a2.keys():
	if i in a1.keys():
		continue
	else:
		s += a2[i]
print(s)


import sys
profits = dict()
for x in range(2):
	n = int(sys.stdin.readline())
	for i in range(n):
		(iD, profit) = map(int, sys.stdin.readline().split())
		if iD in profits:
			profits[iD] = max(profits[iD], profit)
		else:
			profits[iD] = profit
print(sum([b for (a, b) in profits.items()]))


import collections
d = collections.defaultdict(int)
for x in range(int(input())):
	(a, b) = list(map(int, input().split()))
	d[a] = b
for x in range(int(input())):
	(a, b) = list(map(int, input().split()))
	if d[a] < b:
		d[a] = b
print(sum(d.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)


m = {}
for _ in range(2):
	for i in range(int(input())):
		(a, b) = map(int, input().split())
		m[a] = max(m.get(a, 0), b)
print(sum(m.values()))


def choose_sets(m, mseq, n, nseq):
	sorted_mseq = sorted(mseq)
	sorted_nseq = sorted(nseq)
	total = 0
	index_mseq = 0
	index_nseq = 0
	while index_mseq < m and index_nseq < n:
		if sorted_mseq[index_mseq] > sorted_nseq[index_nseq]:
			total += nseq[sorted_nseq[index_nseq]]
			index_nseq += 1
		elif sorted_mseq[index_mseq] < sorted_nseq[index_nseq]:
			total += mseq[sorted_mseq[index_mseq]]
			index_mseq += 1
		else:
			total += max(mseq[sorted_mseq[index_mseq]], nseq[sorted_nseq[index_nseq]])
			index_nseq += 1
			index_mseq += 1
	if index_mseq == m:
		while index_nseq < n:
			total += nseq[sorted_nseq[index_nseq]]
			index_nseq += 1
	else:
		while index_mseq < m:
			total += mseq[sorted_mseq[index_mseq]]
			index_mseq += 1
	return total
chem = int(input())
mseq = {}
for x in range(chem):
	(index, value) = list(map(int, input().split()))
	mseq[index] = value
top = int(input())
nseq = {}
for y in range(top):
	(index, value) = list(map(int, input().split()))
	nseq[index] = value
print(choose_sets(chem, mseq, top, nseq))


n = int(input())
d = {}
for _ in range(n):
	t = input().split()
	d[t[0]] = t[1]
m = int(input())
l = {}
for _ in range(m):
	r = input().split()
	l[r[0]] = r[1]
k = set()
for key in d:
	k.add(key)
f = set()
for key in l:
	f.add(key)
s = 0
p = set(f.intersection(k))
for i in p:
	s += max(int(d[i]), int(l[i]))
for x in k.difference(p):
	s += int(d[x])
for y in f.difference(p):
	s += int(l[y])
print(s)


n = int(input())
n_list = {}
for i in range(n):
	(a, b) = [int(x) for x in input().split()]
	n_list[a] = b
m = int(input())
m_list = {}
for i in range(m):
	(a, b) = [int(x) for x in input().split()]
	m_list[a] = b
income = 0
for i in n_list:
	income += n_list[i]
for i in m_list:
	if i in n_list:
		if m_list[i] > n_list[i]:
			income -= n_list[i]
			income += m_list[i]
	else:
		income += m_list[i]
print(income)


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()))


ans = {}
n = int(input())
for i in range(n):
	f = list(map(int, input().split()))
	if f[0] in ans:
		if f[1] > ans[f[0]]:
			ans[f[0]] = f[1]
	else:
		ans[f[0]] = f[1]
n = int(input())
for i in range(n):
	f = list(map(int, input().split()))
	if f[0] in ans:
		if f[1] > ans[f[0]]:
			ans[f[0]] = f[1]
	else:
		ans[f[0]] = f[1]
print(sum((n for n in ans.values())))


from collections import defaultdict
n = int(input())
a = defaultdict(int)
for i in range(n):
	(u, v) = [int(item) for item in input().split()]
	a[u] = v
m = int(input())
b = defaultdict(int)
for j in range(m):
	(u, v) = [int(item) for item in input().split()]
	b[u] = v
aa = sorted(a.keys(), reverse=True)
bb = sorted(b.keys(), reverse=True)
aaa = []
bbb = []
answer = 0
for i in range(n):
	if aa[i] not in b:
		answer += a[aa[i]]
for i in range(m):
	answer += max(a[bb[i]], b[bb[i]])
print(answer)


choosen = {}
soma = 0
n = int(input())
i = 0
while i < n:
	(element, usage) = [int(i) for i in input().split()]
	choosen[element] = usage
	i += 1
m = int(input())
i = 0
while i < m:
	(element, usage) = [int(i) for i in input().split()]
	if element not in choosen:
		choosen[element] = usage
	elif choosen[element] < usage:
		choosen[element] = usage
	i += 1
for i in choosen:
	soma += choosen[i]
print(soma)


def main():
	n = int(input())
	a = [list(map(int, input().split())) for i in range(n)]
	m = int(input())
	for i in range(m):
		a.append(list(map(int, input().split())))
	a = sorted(a)
	count = 0
	i = 0
	while i < n + m:
		if i + 1 < n + m and a[i][0] == a[i + 1][0]:
			count += max(a[i][1], a[i + 1][1])
			i += 1
		else:
			count += a[i][1]
		i += 1
	print(count)
main()


n = int(input())
pair_n = []
for i in range(n):
	element = list(map(int, input().split()))
	pair_n.append(element)
m = int(input())
pair_m = []
for i in range(m):
	element = list(map(int, input().split()))
	pair_m.append(element)
(pair_n, pair_m) = (sorted(pair_n), sorted(pair_m))
(N, M) = (0, 0)
ans = 0
while N != n or M != m:
	if N == n:
		ans += pair_m[M][1]
		M += 1
	elif M == m:
		ans += pair_n[N][1]
		N += 1
	elif pair_n[N][0] < pair_m[M][0]:
		ans += pair_n[N][1]
		N += 1
	elif pair_n[N][0] > pair_m[M][0]:
		ans += pair_m[M][1]
		M += 1
	else:
		ans += max(pair_n[N][1], pair_m[M][1])
		N += 1
		M += 1
print(ans)


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)


n = int(input())
c1 = {}
set1 = set()
for _ in range(n):
	(t1, t2) = map(int, input().split())
	c1[t1] = t2
	set1.add(t1)
m = int(input())
for __ in range(m):
	(t3, t4) = map(int, input().split())
	if t3 in set1:
		c1[t3] = max(c1[t3], t4)
	else:
		c1[t3] = t4
res = 0
for i in c1:
	res += c1[i]
print(res)


from collections import defaultdict
e = defaultdict(set)
n = int(input())
for i in range(n):
	(x, price) = [int(x) for x in input().split()]
	e[x].add(price)
m = int(input())
for i in range(m):
	(x, price) = [int(x) for x in input().split()]
	e[x].add(price)
sum = 0
for i in e.values():
	sum += max(i)
print(sum)


n = int(input())
i1 = {}
i2 = {}
sum = 0
for i in range(n):
	(l, k) = map(int, input().split())
	i1[l] = k
m = int(input())
for j in range(m):
	(l, k) = map(int, input().split())
	i2[l] = k
for key in i1:
	if key in i2:
		if i1[key] > i2[key]:
			sum += i1[key]
		else:
			sum += i2[key]
		i2[key] = 0
		i1[key] = 0
	else:
		sum += i1[key]
for key in i2:
	sum += i2[key]
print(sum)


n = int(input())
a = {}
for i in range(n):
	(s, t) = map(int, input().split())
	a[s] = t
n = int(input())
b = {}
for i in range(n):
	(s, t) = map(int, input().split())
	b[s] = t
for i in a.keys():
	if i in b.keys():
		if a[i] > b[i]:
			b[i] = 0
		else:
			a[i] = 0
c = 0
for i in a.keys():
	c += a[i]
for i in b.keys():
	c += b[i]
print(c)


n = int(input())
dic = {}
for i in range(n):
	(ai, xi) = map(int, input().split())
	if ai in dic and xi > dic[ai]:
		dic[ai] = xi
	elif ai not in dic:
		dic[ai] = xi
m = int(input())
for j in range(m):
	(ai, xi) = map(int, input().split())
	if ai in dic and xi > dic[ai]:
		dic[ai] = xi
	elif ai not in dic:
		dic[ai] = xi
print(sum(dic.values()))


Maps = {}
N = int(input())
for n in range(N):
	(i, v) = map(int, input().split())
	if i not in Maps:
		Maps[i] = v
	else:
		Maps[i] = max(Maps[i], v)
M = int(input())
for m in range(M):
	(i, v) = map(int, input().split())
	if i not in Maps:
		Maps[i] = v
	else:
		Maps[i] = max(Maps[i], v)
ans = 0
for m in Maps:
	ans += Maps[m]
print(ans)


from collections import defaultdict
dic = defaultdict(int)
n = int(input())
for i in range(n):
	(a, x) = map(int, input().split())
	dic[a] = x
m = int(input())
for i in range(m):
	(b, y) = map(int, input().split())
	t = dic[b]
	dic[b] = max(t, y)
num = 0
for i in dic.values():
	num += i
print(num)


d = dict()
n = int(input())
for _ in range(n):
	(element, price) = map(int, input().split())
	d[element] = price
m = int(input())
for _ in range(m):
	(element, price) = map(int, input().split())
	if element in d:
		d[element] = max(d[element], price)
	else:
		d[element] = price
print(sum(d.values()))


n = int(input())
chem = {}
for i in range(n):
	(x, y) = [*map(int, input().split())]
	if not x in chem:
		chem[x] = y
	else:
		chem[x] = max(chem[x], y)
m = int(input())
for i in range(m):
	(x, y) = [*map(int, input().split())]
	if not x in chem:
		chem[x] = y
	else:
		chem[x] = max(chem[x], y)
ans = 0
for i in chem:
	ans += chem[i]
print(ans)


n = int(input())
a = {}
ans = 0
for i in range(n):
	s = input().split()
	z = int(s[1])
	a[s[0]] = z
	ans += z
m = int(input())
for i in range(m):
	s = input().split()
	z = int(s[1])
	x = a.get(s[0], False)
	if x == False:
		ans += z
	elif z > x:
		ans -= x
		ans += z
print(ans)


from collections import defaultdict
n = int(input())
a = defaultdict(int)
for i in range(n):
	x = list(map(int, input().split(' ')))
	a[x[0]] = x[1]
m = int(input())
for i in range(m):
	x = list(map(int, input().split(' ')))
	if x[0] in a.keys():
		if a[x[0]] < x[1]:
			a[x[0]] = x[1]
	else:
		a[x[0]] = x[1]
print(sum(a.values()))


n = int(input())
a = []
b = []
for i in range(n):
	a.append(list(map(int, input().split())))
m = int(input())
for i in range(m):
	b.append(list(map(int, input().split())))
i = j = 0
a = sorted(a)
b = sorted(b)
ans = 0
while i < n and j < m:
	if a[i][0] < b[j][0]:
		ans += a[i][1]
		i += 1
	elif a[i][0] > b[j][0]:
		ans += b[j][1]
		j += 1
	else:
		ans += max(a[i][1], b[j][1])
		i += 1
		j += 1
while i < n:
	ans += a[i][1]
	i += 1
while j < m:
	ans += b[j][1]
	j += 1
print(ans)


dic = dict()
n = int(input())
for i in range(n):
	tmp = input().split()
	dic[tmp[0]] = int(tmp[1])
m = int(input())
for j in range(m):
	tmp = input().split()
	if tmp[0] not in dic:
		dic[tmp[0]] = int(tmp[1])
	elif dic[tmp[0]] < int(tmp[1]):
		dic[tmp[0]] = int(tmp[1])
sum = 0
for k in dic:
	sum = sum + dic[k]
print(sum)