N = int(input())
A = list(map(int, input().split()))
Q = int(input())
D = {}
for i in A:
  if i in D:
    D[i] += 1
  else:
    D[i] = 1
for i in range(Q):
  s = 0
  bi, ci = map(int, input().split())
  if bi in D:
    if ci in D:
      D[ci] += D[bi]
    else:
      D[ci] = D[bi]
    del D[bi]
  for j in D:
    s += j * D[j]
  print(s)