N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
a = sum(A)
for i in range(Q):
    BC = [int(x) for x in input().split()]
    B = BC[0]
    C = BC[1]
    t = A.count(B)
    A = [C if k == B else k for k in A]
    a += t*(C-B)
    print(a)