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

from collections import Counter

su=sum(a)
cnt=Counter(a)

for i in range(q):
    b,c = map(int,input().split())
    su += (c-b)*cnt[b]
    cnt[c]+=cnt[b]
    cnt[b]=0
    print(su)