n = int(input())
ls = list(map(int,input().split()))
q = int(input())
freqlist = dict()
for i in range(n):
	if ls[i] not in freqlist:
		freqlist[ls[i]] = 1
	else:
		freqlist[ls[i]] += 1


total = sum(ls)
setls = list(set(ls))

for i in setls:
	freqlist[i] = ls.count(i)

for i in range(q):
	a,b = input().split()
	a,b = int(a), int(b)
	if a in freqlist:
		temp = total - freqlist[a]*a
		temp += b * freqlist[a]
		print(temp)
		total = temp
		if b in freqlist:
			freqlist[b] += freqlist[a]
			freqlist.pop(a)
		else:
			k = freqlist.pop(a)
			freqlist[b] = k
	else:
		print(total)

