N = int(input())
A = list(map(str, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for i in range(Q)] 

a =""
for i in range(N-1):
  a += A[i]+"." 
a += A[N-1]

for i in BC:
  B = str(i[0])
  C = str(i[1])
  if B in a:
    a = a.replace(B,C)
  b = a
  print(sum(list(map(int, b.split(".")))))
  