n = int(input())
a = list(map(int, input().split()))
q = int(input())
change = [list(map(int, input().split())) for i in range(q)]
sm = sum(a)
tmp = 0
for i in range(q):
    sm += a.count(change[i][0]) * (change[i][1] - change[i][0])
    print(sm)
    a = [change[i][1] if j == change[i][0] else j for j in a]