n=int(input())
member = [0]*(10**5+1)

li = list(map(int,input().split()))
for i in range(n):
  member[li[i]] +=1

q = int(input())
for j in range(q):
  x,y = input().split()
  x=int(x)
  y=int(y)
  member[y]+=member[x]
  member[x]=0
  total = 0
  for k in range(10**5+1):
    total += k*member[k]
  print(total)