collections.Counter() 

 A counter is a container that stores elements as dictionary keys, and their counts are stored as dictionary values.

 Sample Code 

>>> from collections import Counter
>>> 
>>> myList = [1,1,2,3,4,5,3,2,3,4,2,1,2,3]
>>> print Counter(myList)
Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1})
>>>
>>> print Counter(myList).items()
[(1, 3), (2, 4), (3, 4), (4, 2), (5, 1)]
>>> 
>>> print Counter(myList).keys()
[1, 2, 3, 4, 5]
>>> 
>>> print Counter(myList).values()
[3, 4, 4, 2, 1]

Task

$\textit{Rightu}$ is a shoe shop owner. His shop has $\mbox{X}$ number of shoes. 

He has a list containing the size of each shoe he has in his shop. 

There are $N$ number of customers who are willing to pay $x_i$ amount of money only if they get the shoe of their desired size.

Your task is to compute how much money $\textit{Rightu}$ earned.

Input Format

The first line contains $\mbox{X}$, the number of shoes. 

The second line contains the space separated list of all the shoe sizes in the shop.

The third line contains $N$, the number of customers. 

The next $N$ lines contain the space separated values of the $\textit{shoe size}$ desired by the customer and $x_i$, the price of the shoe.

Constraints

$0<X<10^3$ 

$0<N\leq10^3$ 

$20<x_i<100$ 

$2<shoe size<20$

Output Format

Print the amount of money earned by $\textit{Rightu}$. 

Sample Input
10
2 3 4 5 6 8 7 6 5 18
6
6 55
6 45
6 55
4 40
18 60
10 50

Sample Output
200

Explanation

Customer 1: Purchased size 6 shoe for $55. 

Customer 2: Purchased size 6 shoe for $45. 

Customer 3: Size 6 no longer available, so no purchase. 

Customer 4: Purchased size 4 shoe for $40. 

Customer 5: Purchased size 18 shoe for $60. 

Customer 6: Size 10 not available, so no purchase.

Total money earned = $55+45+40+60=$ $\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\$

Difficulty: EASY

Starter code:

Solutions:
from collections import Counter
x = int(input())
sizes = list(map(int, input().split()))
n = int(input())
sizes = Counter(sizes)
pr = 0
for i in range(n):
	(sz, pz) = map(int, input().split())
	if sizes[sz]:
		sizes[sz] -= 1
		pr += pz
print(pr)


from collections import Counter
c = int(input())
allsize = Counter(map(int, input().split()))
total = 0
for i in range(int(input())):
	(size, cost) = map(int, input().split())
	if size in allsize.keys():
		allsize[size] -= 1
		if allsize[size] < 0:
			cost = 0
		total += cost
print(total)


from collections import Counter
input()
C = Counter(input().split())
I = [input().split() for _ in range(int(input()))]
s = 0
for x in I:
	if C[x[0]] > 0:
		C[x[0]] -= 1
		s += int(x[1])
print(s)


X = int(input())
N = list(map(int, input().split(' ')))
xi = int(input())
total_earn = 0
for x in range(xi):
	(size, amount) = map(int, input().split(' '))
	if size in N:
		N.remove(size)
		total_earn += amount
print(total_earn)


from operator import itemgetter
import operator
from collections import Counter
n = int(input())
s = Counter(list(map(int, input().split())))
m = int(input())
su = 0
for i in range(m):
	ss = list(map(int, input().split()))
	if s[ss[0]] != 0:
		su += ss[1]
		s[ss[0]] -= 1
print(su)


from collections import Counter
input()
shoes = Counter(list(map(int, input().strip().split())))
customers = int(input().strip())
earning = 0
for i in range(customers):
	inp = [int(x) for x in input().strip().split()]
	if inp[0] in shoes and shoes[inp[0]] > 0:
		earning += inp[1]
		shoes[inp[0]] -= 1
print(earning)


from collections import Counter

def __starting_point():
	X = int(input().strip())
	shoes = list(map(int, input().strip().split()))
	N = int(input().strip())
	result = 0
	warhs = Counter(shoes)
	for _ in range(N):
		(size, money) = map(int, input().strip().split(' '))
		if warhs[size] > 0:
			result += money
			warhs[size] -= 1
	print(result)
__starting_point()


import collections
_ = input()
shoe_sizes = [int(x) for x in input().split()]
counter = collections.Counter(shoe_sizes)
N = int(input())
acc = 0
for x in range(N):
	(size, price) = input().split()
	if counter[int(size)] > 0:
		counter[int(size)] -= 1
		acc += int(price)
print(str(acc))


from collections import Counter
a = int(input())
b = list(map(int, input().split()))
d = Counter(b)
count = 0
c = int(input())
for i in range(c):
	(e, f) = list(map(int, input().split()))
	if e in d:
		if d[e] > 0:
			count += f
			d[e] -= 1
print(count)


from collections import Counter
input()
shoes = Counter(map(int, input().split()))
result = 0
for i in range(int(input())):
	(k, p) = map(int, input().split())
	if shoes[k] > 0:
		result += p
		shoes[k] -= 1
print(result)


from collections import Counter
input()
shoes = map(int, input().split())
counts = Counter(shoes)
earnings = 0
n = int(input())
for _ in range(n):
	(size, price) = map(int, input().split())
	if size in counts and counts[size] > 0:
		earnings += price
		counts[size] -= 1
print(earnings)


input()
shoesSizes = list(map(int, input().split()))
N = int(input())
totalMoney = 0
for i in range(N):
	(size, money) = map(int, input().split())
	if shoesSizes.count(size) != 0:
		totalMoney += money
		shoesSizes.remove(size)
print(totalMoney)


from collections import Counter
x = int(input())
s = list(map(int, input().split()))
sc = dict(Counter(s))
m = 0
n = int(input())
for ii in range(n):
	(ss, p) = input().split()
	(ss, p) = (int(ss), int(p))
	if ss in sc:
		b = sc[ss]
		if b > 0:
			m += p
			sc[ss] = b - 1
print(m)


from collections import Counter
tSizes = int(input())
sizes = list(map(int, input().split()))
counter = Counter(sizes)
totalPrice = 0
for i in range(0, int(input())):
	(size, price) = map(int, input().split())
	if counter[size] > 0:
		counter[size] -= 1
		totalPrice += price
print(totalPrice)


from collections import Counter
X = int(input())
shoes = list(map(int, input().split()))
N = int(input())
cnt = Counter(shoes)
money = 0
for _ in range(N):
	(size, price) = map(int, input().split())
	if cnt[size] > 0:
		money += price
		cnt[size] -= 1
print(money)


from collections import Counter
n = int(input())
shoe_sizes = list(map(int, input().split()))
shoe_sizes = Counter(shoe_sizes)
money = 0
m = int(input())
for i in range(m):
	(size, cash) = tuple(map(int, input().split()))
	if shoe_sizes[size] != 0:
		shoe_sizes[size] -= 1
		money += cash
print(money)


from collections import Counter
l = int(input())
C = Counter(map(int, input().split()))
n = int(input())
money = 0
for i in range(0, n):
	(s, m) = map(int, input().split())
	if C[s] > 0:
		money += m
		C[s] = C[s] - 1
print(money)


from collections import Counter
n = int(input())
l = list(map(int, input().split()))
cus = int(input())
c = Counter(l)
tot = 0
for i in range(cus):
	p = list(map(int, input().split()))
	if p[0] in c:
		if c[p[0]] > 0:
			tot = tot + p[1]
			c[p[0]] -= 1
print(tot)


from collections import Counter
x = int(input())
sizes = Counter(list(map(int, input().split())))
n = int(input())
money = 0
for i in range(0, n):
	inputs = input().split()
	size = int(inputs[0])
	price = int(inputs[1])
	count = sizes[size]
	if count > 0:
		money += price
		sizes[size] = count - 1
print(money)


from collections import Counter
x = int(input())
sizes = list(map(int, input().split()))
c = int(input())
money = 0
for i in range(c):
	cnt = Counter(sizes)
	val = input().split()
	s = int(val[0])
	if cnt[s] > 0:
		cnt[s] -= 1
		sizes.remove(s)
		money += int(val[1])
print(money)


from collections import Counter
X = int(input())
shoes = list(map(int, input().strip().split()))
N = int(input())
shoedict = Counter(shoes)
revenue = 0
for _ in range(N):
	(size, price) = map(int, input().strip().split())
	if shoedict[size] != 0:
		revenue += price
		shoedict[size] -= 1
print(revenue)


from collections import Counter
ints = lambda s: map(int, s.split())
_ = int(input())
zs = Counter(ints(input()))
t = 0
for _ in range(int(input())):
	(z, x) = ints(input())
	if z in zs:
		zs -= Counter([z])
		t += x
print(t)


from collections import Counter
k = int(input())
L = list(map(int, input().strip().split()))
S = Counter(L)
T = int(input())
money = 0
while T > 0:
	temp = list(map(int, input().split()))
	if temp[0] in S.keys():
		if S[temp[0]] > 0:
			money = money + temp[1]
			S[temp[0]] = S[temp[0]] - 1
	T = T - 1
print(money)


from collections import Counter
Tnum_Shoes = int(input())
size_shoes = [int(x) for x in input().split()]
money = 0
shoe_list = Counter(size_shoes)
for i in range(int(input())):
	sp = input().split()
	if shoe_list[int(sp[0])] > 0:
		money += int(sp[1])
		shoe_list[int(sp[0])] -= 1
print(money)


n = int(input())
size = list(map(int, input().split()))
c = int(input())
price = 0
for i in range(c):
	(s, p) = input().split()
	s = int(s)
	p = int(p)
	if s in size:
		price = price + p
		size.remove(s)
print(price)


from collections import Counter
no_of_shoes = int(input())
shoes = Counter(list(map(int, input().split())))
no_of_cus = int(input())
profits = 0
for _ in range(no_of_cus):
	(size, money) = map(int, input().split())
	if size in shoes and shoes[size] > 0:
		profits = profits + money
		shoes[size] = shoes[size] - 1
print(profits)


from collections import Counter
numShoes = int(input())
shoeSizes = [int(size) for size in input().split()]
shoeSizeCount = Counter(shoeSizes)
numCustomers = int(input())
custBusiness = {}
for i in range(numCustomers):
	custBusiness[i + 1] = [int(num) for num in input().split()]
money = 0
for i in range(numCustomers):
	if custBusiness[i + 1][0] in shoeSizeCount and shoeSizeCount[custBusiness[i + 1][0]] > 0:
		shoeSizeCount[custBusiness[i + 1][0]] -= 1
		money += custBusiness[i + 1][1]
print(money)


from collections import Counter
choose_number = int(input())
chooses_size = [int(i) for i in input().split(' ')]
chooses_count = Counter(chooses_size)
client_number = int(input())
total = 0
clients = []
for i in range(client_number):
	clients.append([int(i) for i in input().split(' ')])
for (size, price) in clients:
	if size in chooses_count and chooses_count[size] > 0:
		total += price
		chooses_count[size] -= 1
print(total)


from collections import Counter
nbx = int(input())
shoes = Counter(list(map(int, input().split())))
nb = int(input())
money = 0
for _ in range(nb):
	(size, price) = map(int, input().split())
	if shoes[size]:
		if 0 < shoes[size]:
			shoes[size] -= 1
			money += price
print(money)


from collections import Counter
X = int(input())
shoes = [int(i) for i in input().split()]
shoes_c = Counter(shoes)
N = int(input())
ans = 0
for i in range(N):
	(size, x) = map(int, input().split())
	if shoes_c[size] > 0:
		ans += x
		shoes_c[size] -= 1
print(ans)


from collections import Counter
input()
c = Counter(map(int, input().split()))
t = 0
for i in range(int(input())):
	(s, p) = map(int, input().split())
	if c[s] != 0:
		c[s] -= 1
		t += p
print(t)


from collections import Counter
input()
mylist = map(int, input().split())
mydict = Counter(mylist)
n = int(input())
earn = 0
for _ in range(n):
	(mysize, myprice) = map(int, input().split())
	if mysize in mydict and mydict[mysize] > 0:
		earn += myprice
		mydict[mysize] -= 1
print(earn)


from collections import Counter
_ = input()
counter = Counter(map(int, input().split()))
earned = 0
for _ in range(int(input())):
	(size, price) = map(int, input().split())
	if counter[size] > 0:
		counter[size] -= 1
		earned += price
print(earned)


from collections import Counter
x = int(input())
sizes = Counter(map(int, input().split()))
n = int(input())
ssx = [tuple(map(int, input().split())) for _ in range(n)]
earn = 0
for s in ssx:
	if sizes[s[0]] > 0:
		earn += s[1]
		sizes[s[0]] -= 1
print(earn)


import collections
N = int(input())
D = collections.defaultdict(int)
for k in [int(x) for x in input().split()]:
	D[k] += 1
total = 0
for i in range(int(input())):
	(size, price) = [int(x) for x in input().split()]
	if D[size] > 0:
		total += price
		D[size] -= 1
print(total)


from collections import Counter
__author__ = 'namii'
X = int(input())
shoes = Counter(map(int, input().split()))
N = int(input())
profits = 0
for line in range(N):
	(shoe_size, price) = list(map(int, input().split()))
	if shoes[shoe_size] > 0:
		profits += price
		shoes[shoe_size] -= 1
print(profits)


from collections import Counter
No_of_shoes = int(input())
size_of_shoes = input().split()
cnt_of_each_shoes_with_sizes = Counter(size_of_shoes)
No_of_customers = int(input())
size_and_prize = []
Total_Sum = []
for i in range(No_of_customers):
	size_and_prize.append(input().split())
for j in size_and_prize:
	if j[0] in cnt_of_each_shoes_with_sizes and cnt_of_each_shoes_with_sizes[j[0]] > 0:
		Total_Sum.append(int(j[1]))
		cnt_of_each_shoes_with_sizes[j[0]] -= 1
	else:
		Total_Sum.append(0)
print(sum(Total_Sum))


from collections import Counter
input()
shoes_sizes = list(map(int, str(input()).split(' ')))
shoes = Counter(shoes_sizes)
customers = int(input())
profit = 0
for i in range(customers):
	order = list(map(int, str(input()).split(' ')))
	(shoe_size, price) = (order[0], order[1])
	if shoes[shoe_size] > 0:
		profit += price
		shoes[shoe_size] -= 1
print(profit)


from collections import Counter
x = int(input())
shoes = Counter(map(int, input().split()))
n = int(input())
total = 0
for _ in range(n):
	(size, x) = input().split()
	if shoes[int(size)]:
		total += int(x)
		shoes.subtract([int(size)])
print(total)


n = int(input())
shoes = []
dic = {}
amount = 0
for x in input().split(' '):
	if len(shoes) < n:
		shoes.append(int(x))
cust = int(input())
for i in range(cust):
	(size, price) = input().split(' ')
	size = int(size)
	if size in shoes:
		shoes.pop(shoes.index(size))
		amount = amount + int(price)
	else:
		continue
print(amount)


from collections import Counter
numShoes = int(input())
shoes = Counter([int(j) for j in input().split()])
numCustomers = int(input())
income = 0
for cust in range(numCustomers):
	(size, price) = [int(j) for j in input().split()]
	if shoes[size] >= 1:
		income += price
		shoes[size] -= 1
print(income)


from collections import Counter
numshoes = int(input())
sizeslist = map(int, input().split())
sizes = Counter(sizeslist)
numcust = int(input())
total = 0
for cust in range(0, numcust):
	(sz, price) = tuple(map(int, input().split()))
	if sizes[sz] > 0:
		total = total + price
		sizes[sz] -= 1
print(total)


from collections import Counter as c
n = int(input())
shoes = [int(x) for x in input().split()]
count = c(shoes)
sale = 0
customers = int(input())
for _ in range(customers):
	array = [int(x) for x in input().split()]
	if array[0] in count.keys() and count[array[0]] > 0:
		sale += array[1]
		count[array[0]] -= 1
print(sale)


from collections import Counter
n = int(input())
shoes = Counter(map(int, input().split()))
m = int(input())
sum = 0
for i in range(m):
	(s, pr) = map(int, input().split())
	if shoes[s]:
		sum += pr
		shoes[s] -= 1
print(sum)


from collections import Counter
X = int(input())
D = Counter(list(map(int, input().split())))
N = int(input())
Amount = 0
while N > 0:
	L = list(map(int, input().split()))
	if D[L[0]] > 0:
		Amount += L[1]
		D[L[0]] -= 1
	N -= 1
print(Amount)


x = int(input())
shoe_size = list(map(int, input().split()))
n = int(input())
sell = 0
for i in range(n):
	(s, p) = list(map(int, input().split()))
	if s in shoe_size:
		sell = sell + p
		shoe_size.remove(s)
print(sell)


from collections import Counter
input()
c = Counter(map(int, input().split()))
total = 0
n = int(input())
for _ in range(n):
	(s, p) = map(int, input().split())
	if s in c.keys() and c[s] > 0:
		c[s] -= 1
		total += p
print(total)


from collections import Counter
_ = input()
xs = map(int, input().split())
stats = Counter(xs)
N = int(input())
tot = 0
for i in range(N):
	(sz, money) = map(int, input().split())
	if sz in stats and stats[sz] >= 1:
		stats[sz] -= 1
		tot += money
print(tot)


from collections import Counter
a = int(input())
b = list(map(int, input().split()))
c = int(input())
d = []
for i in range(c):
	d.append(list(map(int, input().split())))
shoecnt = Counter(b)
sum = 0
for i in d:
	if shoecnt.get(i[0], 0) >= 1:
		sum = sum + i[1]
		shoecnt[i[0]] = shoecnt.get(i[0]) - 1
print(sum)


from collections import Counter
from collections import defaultdict
d = defaultdict(list)
total = 0
shoes = int(input())
k = Counter(list(map(int, input().split())))
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	d[a].append(b)
for (i, j) in d.items():
	if k[i] >= len(j):
		total += sum(j)
	else:
		total += sum(j[0:k[i]])
print(total)


input()
sizes_list = [int(x) for x in input().split()]
sum = 0
for _ in range(int(input())):
	(size, price) = input().split()
	size = int(size)
	price = int(price)
	if size in sizes_list:
		sum += price
		sizes_list.remove(size)
print(sum)


from collections import Counter
x = int(input())
sizel = list(map(int, input().split()))
tc = int(input())
sized = Counter(sizel)
s = 0
c = 0
while c < tc:
	(size, rate) = map(int, input().split())
	if sized[size] > 0:
		sized[size] -= 1
		s += rate
	else:
		c += 1
		continue
	c += 1
print(s)


from collections import Counter
shoes = int(input())
shoesizes = [int(k) for k in input().split()]
shoecounter = Counter(shoesizes)
cust = int(input())
earn = 0
for _ in range(cust):
	(s, price) = [int(f) for f in input().split()]
	if s in shoecounter.keys():
		if shoecounter[s] != 0:
			shoecounter[s] -= 1
			earn += price
print(earn)


from collections import Counter
X = int(input())
av = list(map(int, input().split()))
prof = 0
for i in range(int(input())):
	cus = list(map(int, input().split()))
	if cus[0] in Counter(av).keys():
		prof += cus[1]
		av.remove(cus[0])
print(prof)


from collections import Counter
input()
shoeses = Counter(map(int, input().split()))
total = 0
for _ in range(int(input())):
	(shoes, price) = map(int, input().split())
	if shoeses[shoes] > 0:
		total += price
		shoeses[shoes] -= 1
print(total)


from collections import Counter
(totShoes, stock, custCount) = (input(), input().split(), input())
inventory = Counter(stock)
sales = 0
for customer in range(int(custCount)):
	(request, price) = input().split()
	if inventory[request] > 0:
		sales += int(price)
		inventory[request] -= 1
print(str(sales))


from collections import Counter
X = int(input())
shoes_size = Counter(map(int, input().split()))
N = int(input())
costumers = [list(map(int, input().split())) for n in range(N)]
total = 0
for costumer in costumers:
	if costumer[0] in shoes_size:
		if shoes_size[costumer[0]] is not 0:
			shoes_size[costumer[0]] -= 1
			total += costumer[1]
print(total)


from collections import Counter
X = int(input())
SUM = 0
shoes = list(map(int, input().split()))
N = int(input())
shoes = list(Counter(shoes).items())
shoes = list(map(list, shoes))
for i in range(N):
	(L, M) = map(int, input().split())
	for j in shoes:
		if L == j[0] and j[1] != 0:
			SUM = SUM + M
			j[1] = j[1] - 1
print(SUM)


from collections import Counter
X = int(input())
sizes = map(int, input().split())
N = int(input())
sizes_counts = Counter(sizes)
money = 0
for _ in range(N):
	(size, price) = map(int, input().split())
	if sizes_counts[size] > 0:
		money += price
		sizes_counts[size] -= 1
print(money)


from collections import Counter
input()
shoes = list(map(int, input().strip().split()))
N = int(input())
sCount = Counter(shoes)
money = 0
for _ in range(N):
	(size, dollar) = [int(i) for i in input().strip().split()]
	if sCount[size] > 0:
		money += dollar
		sCount[size] -= 1
print(money)


from collections import Counter
num_shoes = int(input())
stock = Counter(map(int, input().split()))
num_customers = int(input())
earned = 0
for _ in range(num_customers):
	(size, price) = list(map(int, input().split()))
	if stock[size]:
		earned += price
		stock[size] -= 1
print(earned)


from collections import Counter
x = int(input())
a = Counter([int(i) for i in input().split()])
n = int(input())
p = []
for i in range(n):
	tmp = input().split()
	p.append([int(tmp[0]), int(tmp[1])])
res = 0
for i in p:
	if a[i[0]] > 0:
		a[i[0]] = a[i[0]] - 1
		res += i[1]
print(res)


import sys
from collections import Counter
X = min(int(sys.stdin.readline().strip()), 10 ** 3)
sizes = Counter(map(int, sys.stdin.readline().strip().split(' ')))
N = min(int(sys.stdin.readline().strip()), 10 ** 3)
total = 0
for _ in range(N):
	transaction = sys.stdin.readline().strip()
	(size, price) = map(int, transaction.split(' '))
	if sizes[size] > 0:
		sizes[size] -= 1
		total += price
print(total)


from collections import Counter
m = int(input())
n = input().split()
q = Counter(n)
c = int(input())
pr = 0
for i in range(c):
	(s, d) = input().split()
	if s in Counter(n).keys() and q[s] > 0:
		pr = pr + int(d)
		q[s] = q[s] - 1
print(pr)


import collections
no_of_shoes = int(input())
sizes_of_shoes = [int(x) for x in input().split(' ')]
no_of_customers = int(input())
inventory = collections.Counter(sizes_of_shoes)
total = 0
for customer in range(no_of_customers):
	(size, price) = [int(x) for x in input().split(' ')]
	if inventory[size]:
		total += price
		inventory[size] -= 1
print(total)


from collections import Counter
X = int(input())
shoes = Counter(list(map(int, input().split())))
N = int(input())
amount = 0
for i in range(N):
	(size, price) = map(int, input().split())
	if shoes[size] > 0:
		shoes[size] -= 1
		amount += price
print(amount)


from collections import Counter
X = int(input())
shoes_size = Counter(list(map(int, input().split())))
N = int(input())
count_money = 0
for _ in range(N):
	(size, price) = map(int, input().split())
	if size in shoes_size.elements():
		count_money += price
		shoes_size[size] -= 1
print(count_money)


from collections import Counter
X = int(input())
c = Counter(map(int, input().split()))
N = int(input())
total = 0
while N > 0:
	N -= 1
	(i, p) = map(int, input().split())
	if c[i] > 0:
		total += p
		c[i] -= 1
print(total)


from collections import Counter
X = int(input())
income = 0
sofs = map(int, input().split())
C = Counter(sofs)
N = int(input())
for i in range(N):
	(size, price) = map(int, input().split())
	if C[size] > 0:
		C[size] -= 1
		income += price
print(income)


from collections import Counter
t = input()
total = 0
sizes = Counter(list(map(int, input().split())))
for _ in range(int(input())):
	good = list(map(int, input().split()))
	if sizes[good[0]] > 0:
		total += good[1]
		sizes[good[0]] -= 1
print(total)


from collections import Counter
n = input()
shoe = Counter(list(input().split()))
k = int(input())
earn = 0
for i in range(k):
	(cus, price) = input().split()
	if shoe[cus] > 0:
		earn += int(price)
		shoe[cus] -= 1
print(earn)


from collections import Counter
x = int(input())
x_list = list(map(int, input().split()))
n = int(input())
price_d = dict(Counter(x_list))
sum = 0
sizes = price_d.keys()
for c in range(n):
	(s, p) = list(map(int, input().split()))
	if s in sizes:
		if price_d[s] > 0:
			sum += p
			price_d[s] -= 1
print(sum)


from collections import Counter
(_, counter) = (input(), Counter(input().split()))
money = 0
for _ in range(int(input())):
	(size, price) = input().split()
	if counter[size] > 0:
		money += int(price)
		counter[size] -= 1
print(money)


from collections import Counter
x = int(input())
sizes = Counter((int(x) for x in input().split(' ')))
n = int(input())
m = 0
for _ in range(n):
	(ss, xi) = [int(x) for x in input().split(' ')]
	if ss in sizes and sizes[ss] > 0:
		m += xi
		sizes[ss] -= 1
print(m)


from collections import Counter
import pprint

def func(a):
	if a != ' ':
		return a
n = input()
m = input().split(' ')
nn = list(map(int, m))
dtr = Counter(nn)
a = input()
ans = 0
for i in range(int(a)):
	(b, c) = input().split(' ')
	if dtr[int(b)] != 0:
		ans += int(c)
		dtr[int(b)] -= 1
print(ans)


from collections import Counter
X = input().strip()
shoe = list(map(int, input().split()))
shoe_tuple_list = Counter(shoe).items()
A = Counter(shoe).keys()
B = Counter(shoe).values()
N = int(input())
sum = 0
for i in range(N):
	c = list(map(int, input().split()))
	(d, e) = (c[0], c[1])
	A = list(A)
	B = list(B)
	if d in A:
		index = A.index(d)
		if B[index] > 0:
			B[index] = B[index] - 1
			sum = sum + e
print(sum)


from collections import Counter
X = int(input())
shoesize = Counter(map(int, input().split()))
s = 0
for _ in range(int(input())):
	(size, val) = map(int, input().split())
	s = s + val if shoesize[size] > 0 else s
	shoesize[size] -= 1
print(s)


from collections import Counter
x = int(input())
size = list(map(int, input().split()))
c = Counter(size)
total = 0
for _ in range(int(input())):
	(n, p) = map(int, input().split())
	if c[n] >= 1:
		total += p
		c[n] = c[n] - 1
print(total)


from collections import Counter
x = int(input())
c = Counter(map(int, input().split()[:x]))
s = 0
for _ in range(int(input())):
	(z, m) = map(int, input().split())
	if z in c and c[z] > 0:
		s += m
		c[z] -= 1
print(s)


input()
shoenums = list(map(int, input().split()))
from collections import Counter
d = dict(Counter(shoenums))
sums = 0
numOfCustomers = int(input())
for i in range(numOfCustomers):
	l = list(map(int, input().split()))
	if l[0] in d and d[l[0]] != 0:
		sums += l[1]
		d[l[0]] -= 1
print(sums)


from collections import Counter
n = int(input())
x = list(map(int, input().split()))
dict = Counter(x)
m = int(input())
sum = 0
for i in range(0, m):
	(siz, price) = list(map(int, input().split()))
	if dict[siz] > 0:
		sum += price
		dict[siz] -= 1
print(sum)


from collections import Counter
x = int(input())
sh = [int(i) for i in input().split()]
shoe = Counter(sh)
sum = 0
n = int(input())
for i in range(0, n):
	ss = input().split()
	s = int(ss[0])
	a = int(ss[1])
	if s in shoe:
		sum = sum + a
		shoe[s] = shoe[s] - 1
		if shoe[s] == 0:
			del shoe[s]
print(sum)


from collections import Counter
x = int(input())
shoes = map(int, input().split())
n = int(input())
c = Counter(shoes)
out = 0
for i in range(n):
	(shoe_size, xi) = map(int, input().split())
	if shoe_size in c:
		out += xi
		c[shoe_size] -= 1
		if c[shoe_size] == 0:
			del c[shoe_size]
print(out)


from collections import Counter
n = int(input())
l = Counter([int(x) for x in input().split()])
m = int(input())
a = []
for i in range(0, m):
	a.append([int(x) for x in input().split()])
sorted(a, key=lambda x: x[1], reverse=True)
b = dict(l)
u = {}
out = 0
for x in a:
	if x[0] in u:
		if u[x[0]] < b[x[0]]:
			out = out + x[1]
			u[x[0]] = u[x[0]] + 1
	elif x[0] in b:
		out = out + x[1]
		u[x[0]] = 1
print(out)


from collections import Counter
input()
shoes = [int(x) for x in input().split()]
n = input()
sum = 0
for _ in range(int(n)):
	inp = input().split()
	if int(inp[0]) in shoes:
		shoes.remove(int(inp[0]))
		sum += int(inp[1])
print(sum)


from collections import Counter
nShoes = int(input())
shoes = Counter(list(map(int, input().split())))
nCustomers = int(input())
gross = 0
for i in range(nCustomers):
	order = list(map(int, input().split()))
	if shoes[order[0]] > 0:
		shoes[order[0]] -= 1
		gross += order[1]
print(gross)


from collections import Counter
n = int(input().strip())
arr = list(map(int, input().strip().split()))
shoes = Counter(arr)
earnings = 0
for i in range(int(input().strip())):
	(size, amount) = map(int, input().strip().split())
	if size in shoes and shoes[size] != 0:
		earnings += amount
		shoes[size] -= 1
print(earnings)


from collections import Counter
n = input()
X = [int(x) for x in input().split()]
amount = 0
for x in range(int(input())):
	(size, money) = input().split()
	if int(size) in X:
		amount += int(money)
		X.remove(int(size))
print(amount)


from collections import Counter
input()
(A, C) = (Counter(input().split()), 0)
for i in range(int(input())):
	B = input().split()
	if A[B[0]] != 0:
		C += int(B[1])
		A[B[0]] -= 1
print(C)


X = int(input())
N = list(map(int, input().split()))
C = int(input())
li = [list(map(int, input().split())) for cust in range(C)]
from collections import Counter
di = Counter(N)
out = 0
for i in li:
	if i[0] in di.keys() and di[i[0]] > 0:
		out = out + i[1]
		di[i[0]] = di[i[0]] - 1
print(out)


from collections import defaultdict
import sys
if sys.version_info[0] >= 3:
	raw_input = input
raw_input()
d = defaultdict(int)
for e in raw_input().split():
	d[int(e)] += 1
r = 0
for _ in range(int(raw_input())):
	(x, y) = map(int, raw_input().split())
	if d[x]:
		d[x] -= 1
		r += y
print(r)


def collection():
	total_price = 0
	m = int(input())
	list = [int(x) for x in input().split()]
	total_customers = int(input())
	for i in range(total_customers):
		(size, price) = [int(x) for x in input().split()]
		if size in list:
			total_price += price
			list.remove(size)
	print(total_price)
collection()


from collections import Counter
input()
c = Counter((int(x) for x in input().split()))
earnings = 0
for _ in range(int(input())):
	(s, p) = map(int, input().split())
	if c[s] > 0:
		c[s] -= 1
		earnings += p
print(earnings)


from collections import Counter
X = int(input())
sizes = Counter(map(int, input().split(' ')))
N = int(input())
am = 0
for _ in range(N):
	(s, x) = list(map(int, input().split(' ')))
	if s in sizes.keys() and sizes[s] > 0:
		am += x
		sizes[s] -= 1
print(am)


from collections import Counter
x = int(input())
sizes = list(map(int, input().split()))
n = int(input())
cnt = Counter(sizes)
total = 0
for i in range(0, n):
	line = list(map(int, input().split()))
	size = int(line[0])
	price = int(line[1])
	if cnt[size] >= 1:
		total += price
		cnt[size] -= 1
print(total)


from collections import Counter
x = int(input())
count = Counter(list((int(i) for i in input().split())))
total = 0
for i in range(int(input())):
	(size, price) = map(int, input().split())
	if size in count.keys():
		total += price
		if count[size] == 1:
			count.pop(size)
		else:
			count[size] -= 1
print(total)


import collections
X = int(input())
shoes = collections.Counter(list(map(int, input().split())))
N = int(input())
sum = 0
for n in range(N):
	(size, price) = map(int, input().split())
	if shoes[size] > 0:
		shoes[size] -= 1
		sum += price
print(sum)


from collections import Counter
x = int(input())
sizes = Counter(map(int, input().split()))
n = int(input())
total = 0
for i in range(n):
	(s, p) = map(int, input().split())
	if sizes[s] > 0:
		total += p
		sizes[s] -= 1
print(total)


input()
ss = list(map(int, input().split()))
t = int(input())
amount = 0
for _ in range(t):
	(thing, price) = map(int, input().split())
	if thing in ss:
		amount += price
		ss.remove(thing)
print(amount)