from collections import Counter
N=int(input())
A=list(map(int,input().split()))
s=sum(A)
Q=int(input())
cnt=Counter(A)
for _ in range(Q):
    b,c=map(int,input().split())
    if b in cnt:
        s-=cnt[b]*b
        s+=cnt[b]*c
        print(s)
        if c in cnt:
            cnt[c]+=cnt[b]
            cnt[b]=0
        else:
            cnt[c]=cnt[b]
    else:
        print(s)