from collections import Counter
N = int(input())
A = list(map(int, input().split()))
AC = Counter(A)
S = sum(A)
Q = int(input())
for _ in range(Q):
  B,C = map(int, input().split())
  if B not in AC:
    pass
  else:
    count = AC[B]
    S = S + (C-B)*count
    if C in AC:
      AC[C] += count
    else:
      AC[C] = count
  print(S)
  
