import sys
input = sys.stdin.readline

# D - Replacing
def get_sum(dic):
	sum = 0
	
	for k, v in dic.items():
		sum += k * v
	
	return sum


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

for ai in a:
	dic.setdefault(ai, 0)
	dic[ai] += 1

for i in range(q):
	b, c = map(int, input().split())

	count = dic.pop(b, 0)
	
	if count > 0:
		dic.setdefault(c, 0)
		dic[c] += count
	
	print(get_sum(dic))