import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
q = int(input())

l = [0] * 100000
m = list(range(1,100001))

for i in range(n):
  l[a[i]-1] += 1
  
for j in range(q):
  b = list(map(int,input().split()))
  l[b[1]-1] += l[b[0]-1]
  l[b[0]-1] = 0
  print(sum(list(map(lambda x,y: x*y, l, m))))
