import collections
n=int(input())
l=list(map(int, input().split()))
q=int(input())
s=sum(l)
d=collections.Counter(l)
for i in range(q):
    a,b=map(int, input().split())
    s=s-(a*d[a])
    s=s+(b*d[a])
    d[b]=d[b]+d[a]
    d[a]=0
    print(s)