import collections
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
l=[]
for _ in range(Q):
    l.append(list(map(int,input().split())))
c = collections.Counter(A)
sum=sum(A)
for i in range(Q):
    if c[l[i][0]] == 0:
        print(sum)
        break
    else:
        sum+=c[l[i][0]]*(l[i][1]-l[i][0])
        c[l[i][1]]+=c[l[i][0]]
        del c[l[i][0]]
        print(sum)
