N = int(input())
A = list(map(int,input().split()))
A.sort()
B = []
C = []
Q = int(input())
for i in range(Q):
    b, c = map(int,input().split())
    for j, a in enumerate(A):
        if a == b:
            A[j] = c
    print(sum(A))
