N = int(input())
X = {}

A = list(map(int, input().split()))

for i in range(N):
  a = A[i]
  if not a in X:
    X[a] = 1
  else:
    X[a] += 1

Q = int(input())
B = []
C = []

ANS = []
for i in range(Q):
  b, c = map(int, input().split())
  if b in X:
    if c in X:
      X[c] += X[b]
    else:
      X[c] = X[b]
    X[b] = 0
  ans = 0
  for i in X.keys():
    ans += i * X[i]
  ANS.append(ans)

for i in range(len(ANS)):
  print(ANS[i])