N = int(input())
A = {}
ans = 0
for a in input().split():
    a = int(a)
    if a not in A:
        A[a] = 1
    else:
        A[a] += 1
    ans += a
    
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    ans += (C-B) * A[B]
    if C not in A:
        A[C] = A[B]
    else:
        A[C] += A[B]
    A[B] = 0
    print(ans)