n = int(input())
a = list(map(int, input().split()))
ad = {}
for i in a:
  if i not in ad:
  	ad[i] = a.count(i)
q = int(input())
ll = []
for i in range(q):
	l = list(map(int, input().split()))
	ll.append(l)
	if l[1] not in ad:
		ad[l[1]] = 0

for i in ll:
  if i[0] in ad:
    ad[i[1]] = ad[i[1]] + ad[i[0]]
    ad[i[0]] = 0
    adsum = 0
    for j in ad:
      adsum += j*ad[j]
    print(adsum)
  else:
    adsum = 0
    for j in ad:
      adsum += j*ad[j]
    print(adsum)