import collections
N = int(input())
A = [int(i) for i in input().split(" ")]
Q = int(input())

for i in range(Q):
    B, C = map(int, input().split(" "))
    while B in A:
        A.remove(B)
        A.append(C)
    print(sum(A))