N = int(input())
A = map(int, input().split())
Q = int(input())



A = list(A)

hashmap = {}
s = 0
for i in range(len(A)):
	s += A[i]
	hashmap[A[i]] = hashmap.get(A[i], {'i': 0, 'v': A[i]}) 
	hashmap[A[i]]['i'] += 1

for _ in range(Q):
	# replace every element whose value is B with C
	B, C = map(int, input().split())

	if B in hashmap:
		num = hashmap[B]['i']
		del hashmap[B]
		s -= num * B
		s += num * C

	hashmap[C] = hashmap.get(C, {'i': 0, 'v': C})
	hashmap[C]['i'] += num
	print(s)