import collections

n=int(input())
a=list(map(int,input().split()))
q=int(input())
s=sum(a)
cc=collections.Counter(a)


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