N = int(input())
A = [str(x) for x in input().split()]
Q = int(input())

l = []
A =','.join(A)
for i in range(Q):
    B, C = map(int, input().split())
    B = str(B)
    C = str(C)
    A = A.replace(B,C)
    l.append(A)

for j in range(Q):
    S = l[j]
    S = S.split(',')
    S = [int(str) for str in S]
    print(sum(S))