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

ans = sum(A)

R = {}
for i in A:
  if i in R:
    R[i] += 1
  else:
    R[i] = 1

for b, c in BC:
  if b in R:
    t = R[b]
    ans += t * (c - b)
    if c in R:
      R[c] += t
    else:
      R[c] = t
  print(ans)