from collections import Counter
N = int(input())
*A, = map(int, input().split())
Q = int(input())
s = sum(A)
c = Counter(A)

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