n = int(input())
a = map(int, input().split())
q = int(input())
table = [[int(i) for i in input().split()] for q in range(q)]
list2 = []
for item in a:
  mod =item.replace(table[0][0],table[0][1])
  list2.append(mod)
print(sum(list2))
list3 =[]
for item2 in list2:
  mod1 = item2.replace(table[1][0],table[1][0])
  list3.append(mod1)
print(sum(list3))
list4 = []
for item3 in list3:
  mod2 = item3.replace(table[2][0],table[2][1])
  list4.append(mod2)
print(sum(list4))


