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

B = []
C = []

l = []

for i in range(Q):
    b, c = list(map(int, input().split()))

    B.append(b)
    C.append(c)

t = 0

for i in A:
    if i > t:
        t = i

for i in range(t + 1):
    l.append(i)
    l[i] = 0

for i in A:
    l[i] = l[i] + 1

for i in range(Q):
    t = l[B[i]]
    l[B[i]] -= t
    l[C[i]] += t

    res = 0
    for num in range(len(l)):
        res += num * l[num]

    print(res)