N=int(input())
A = list(input().split())
Q = int(input())
pairs = [list(input().split()) for i in range(Q)]

for pair in pairs:
#  cnt = 0
#  for j in range(N):
#    if A[j] == pairs[i][0]:
#      A[j] = pairs[i][1]
#    cnt += int(A[j])
  A = [s.replace(pair[0], pair[1]) for s in A]
  print(sum(list(map(int, A))))
#  ret = sum(list(map(int,A)))

