import collections


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