from collections import Counter
N = int(input())
A = input()
C_A = Counter(A.split())
S = 0
for ca in C_A:
  S += int(ca) * C_A[ca]

Q = int(input())
for q in range(Q):
  b, c = input().split()
  A = A.replace(b,c)
  S += (int(c) - int(b)) * C_A[b]
  C_A = Counter(A.split())
  
  print(S)