n = int(input())
num = list(map(int, input().split()))
q = int(input())
change = [0 for i in range(q)]
for i in range(q):
    change[i] = list(map(int, input().split()))

dic = {}
for i in num:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1
ans = 0
for i, j in enumerate(dic):
    ans += j*dic[j]

for i in range(q):
    ans += dic[change[i][0]] * (change[i][1] - change[i][0])
    if change[i][0] in dic:
        if change[i][1] in dic:
            dic[change[i][1]] += dic.pop(change[i][0])
        else:
            dic[change[i][1]] = dic.pop(change[i][0])
    print(ans)