n = int(input())
a = list(map(str, input().split()))
q = int(input())

for i in range(q):
  b, c = map(str, input().split())
  num = ",".join(a)
  num = num.replace(b, c)
  a = num.split(",")
  ans = map((lambda x:int(x)), a)
  print(sum(ans))