n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc=[]
for _ in range(q):
    b,c=map(int,input().split())
    bc.append([b,c])
d={}
for i in range(n):
    if a[i] not in d:
        d[a[i]]=[i]
    else:
        d[a[i]].append(i)

for i in range(q):
    b=bc[i][0]
    c=bc[i][1]
    if b in d:
        for j in d[b]:
            a[j]=c
        if c not in d:
            d[c]=d[b]
        else:
            d[c]+=d[b]
        d[b]=[]
    print(sum(a))

