N = int(input())
A = list(map(int, input().split()))
Q = int(input())
num = []
ans = sum(A)

for i in range(100001):
    if i in A:
        num.append(A.count(i))
    else:
        num.append(0)

for i in range(Q):
    B, C = map(int, input().split())
    ans += (C - B)*num[B]
    num[C] += num[B]
    num[B] = 0

    print(ans)