N = int(input())
A = list(map(int, input().split()))

ans = 0
n = [0] * 10005
for i in range(N):
    n[A[i]] += 1
    ans += A[i]
Q = int(input())
for i in range(Q):
    b, c = map(int, input().split())
    ans += c * n[b] - b * n[b]
    print(ans)
    n[c] += n[b]
    n[b] = 0
