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

table = [0] * 100001

c = 0
s = 0

for i in A:
    table[i] += 1
    s += i

print(su,s)

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