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

x = {a:A.count(a) for a in set(A)}

for _ in range(Q):
    B, C = map(int, input().split())
    x[C] = x.get(C, 0) + x.get(B, 0)
    x[B] = 0
    print(sum(k * v for k, v in x.items()))