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