
import collections
n = int(raw_input())
nums = map(int,raw_input().split(' '))
q = int(raw_input())
s = sum(nums)
h = collections.Counter(nums)
for _ in range(q):
	ov,nv = map(int, raw_input().split(' '))
	h[nv] += h[ov]
	s += h[ov] * (nv - ov)
	
	h[ov] = 0
	print s