import collections
n=int(input())
a=list(map(int,input().split()))
dic=collections.Counter(a)
m=int(input())

s=sum(a)

for i in range(m):
    p,q=map(int,input().split())
    s=s-p*dic[p]+q*dic[p]
    dic[q]+=dic[p]
    dic[p]=0
    print(s)
