n = int(input())
a = list(map(int,input().split()))
b = [0 for _ in range(100001)]
for x in a:
  b[x] += 1
ans = sum(a)
q = int(input())
for _ in range(q):
  x,y = map(int,input().split())
  ans += (y-x) * b[x]
  b[x],b[y] = 0,b[x]+b[y]
  print(ans)