N = int(input())
A = [int(a) for a in input().split()]
X = [0] * 100001
S = sum(A)
for a in A:
    X[a] += 1

for _ in range(int(input())):
    b, c = map(int, input().split())
    S += (c - b) * X[b]
    X[c] += X[b]
    X[b] = 0
    print(S)