def main():
size = 0
size = 0
res = 0
shoes = int(input())
arr = input().split(" ")
cus = int(input())
for i in range(cus):
size, pur = input().split()
if size in arr:
res += int(pur)
arr.remove(size)
print(res)
#for i in n:
# if i.isalnum():
if __name__ == "__main__":
main()