n = int(input())
A = list(map(int, input().split()))
q = int(input())

from collections import Counter
C = Counter(A)
S = sum(A)
for i in range(q):
    b, c = map(int, input().split())
    if b in C:
        vb = C[b]
        S -= b*vb
        S += c*vb
        print(S)
        if c in C:
            C[c] += vb
        else:
            C[c] = vb
    else:
        print(S)
