n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = []
ans = []
for i in range(q):
  b, c = map(str, input().split())
  a = [str(n) for n in a]
  s = ''.join(a)
  s = s.replace(b, c)
  a = list(s)
  a = [int(n) for n in a]
  ans.append(sum(a))
for i in range(q):
  print(ans[i])