N = int(input())
A = sorted(map(int, input().split()))
ans = sum(A)
nums = [A.count(num) for num in range(10 ** 5)]

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