import collections
N=int(input())
A=list(map(int,input().split()))
S=sum(A)
D=collections.Counter(A)
Q=int(input())
for i in range(Q):
    B,C=map(int,input().split())
    S+=(C-B)*D[B]
    D[C]+=D[B]
    D[B]=0
    print(S)
