from collections import Counter

n = int(input())
al = list(map(int, input().split()))
al_sum = sum(al)
al_c = Counter(al)
# print(al_c)

q = int(input())
for _ in range(q):
    bi, ci = map(int, input().split())
    al_sum += al_c[bi]*(ci - bi)

    al_c.update({ci:al_c[bi]})
    del al_c[al_c[bi]]

    print(al_sum)
