n=int(input())
a=list(map(int,input().split()))
q=int(input())
d=dict()
for i in a:
    if i not in d:
        d[i]=1
    else:
        d[i]+=1
ans=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    if b not in d:
        continue
    if c not in d:
        d[c]=0
    d[c]+=d[b]
    ans+=(c-b)*d[b]
    print(ans)
    d[b]=0
